找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

python索引/切片/字符串/列表

[复制链接]
索引
正向索引:012345
a = abcdef
反向索引:-6 -5 -4 -3 -2 -1

切片:是指对操作的对象截取其中一部分的操作,字符串,列表,元组都支持切片操作
(起始位:结束位:隔位)
包含起始不包含结束,隔位不能为0可以为1,1表示不隔位
a = "abcdef"
正向索引:012345
反向索引:-6 -5 -4 -3 -2 -1

print(a[0:3])#abc 从索引值0开始取,到结束索引值3  取0-2里面的值
print(a[3])#d 取当前索引值为3的对应的值
print(a[-3])#d 取当前索引值为-3的对应的值
#print(a[0:6])
print(a[0:])#abcdef
print(a[0:-1])#abcde
print(a[0:6:3])#ad
print(a[::1])#abcdef
print(a[::-1])#fedcba 面试题重点 把字符串中的值进行反向输出
print(a[-2:-6:-2])#ec

字符串
定义:用单引号或双引号来定义
类型:str
a = 'mksdw'
print(type(a))#打印类型为<class 'str'>
b ="ansandi"
print(type(b))#打印类型为<class 'str'>

字符串中一些常用函数

首字母大写
a ="hello world" #定义一个字符串
print(a)#hello world
print(a.capitalize())#Hello world
st =a.capitalize()
print(st)#Hello world

统计
a ="hello world" #定义一个字符串
count统计字符串元素出现的次数
print(a.count('l'))#3
b = a.count("l")
print(b)#3

拼接
a ="helloworld"
print("*".join(a))#h*e*l*l*o*w*o*r*l*d

b=['h','e','l','l','o']
print(b)#['h', 'e', 'l', 'l', 'o']
print("".join(b))#hello

分割
注意:通过split分割后的结果返回的类型是列表的类型list
a ="hello,world"
print(type(a))#<class 'str'>
b = a.split("l")
print(b)#['he', '', 'o,wor', 'd']
print(type(b))#<class 'list'>

print("".join(b))#heo,word
print(type("".join(b)))#<class 'str'>

删除字符
a ="helloworld"#定义一个字符串
strip 删除字符串的开头和结尾的元素
lstrip 删除字符串开头的元素,不能删除结尾的
rstrip 删除字符结尾的元素,不能删除开头的
print(a.strip("h"))#elloworld
print(a.strip("hello"))#world
print(a.lstrip("h"))#elloworld
print(a.rstrip("d"))#helloworl

判断字符是以什么开头结束,返回的是布尔值
a ="helloworld"#定义一个字符串
a1 ='werewrete'
startwith判断是否以某某开头,是返回true 反之false
endswith 判断是否以某某结尾,是返回true 反之false
print(a.startswith("hello"))#True
print(a1.startswith("hello"))#False
print(a.endswith("rld"))#True
print(a1.endswith("rld"))#False

获取指定值的索引值
a ="helloworld"#定义一个字符串
find 从左边开始
rfind 从右开始
print(a.find("o"))#4
print(a.rfind("o"))#6

替换函数
a ="helloworld"#定义一个字符串
print(a.replace("l","3"))#he33owor3d 默认替换所有
print(a.replace("l","5",1))#he5loworld 把l换成5 1表示次数
print(a.replace("helloworld","小翁"))#小翁替换所有
print(a.replace(a,"小翁1"))#小刘替换所有

把字符串中的大写字母变成小写
a ="HEllO"#定义一个字符串
print(a.lower())#hello

把小写字母变成大写
a ="HEllO"#定义一个字符串
print(a.upper())#HELLO

判断是否全部为数字,返回布尔值
a ='h12sd'
a1='1255'
print(a.isdigit())#False
print(a1.isdigit())#True

判断是否全部为字母,返回布尔值
a ='h12sd'
a1='1255'
a2='adASD'
print(a.isalpha())#False
print(a1.isalpha())#False
print(a2.isalpha())#True

判断是否为全数字或全字母或数字加字母
a ='a75cc'
a1='2566'
a2='HFJG'
a3="SDD@"
a4="zz12#"
print(a.isalnum())#True
print(a1.isalnum())#True
print(a2.isalnum())#True
print(a3.isalnum())#False
print(a4.isalnum())#False

判断首字母是否为大写,其他小写
a="A"
a1="MMcdcd"
a2="A1234"
a3='A@3'
a4="aAbh"
a5="Aabggg"
print(a.istitle())#True
print(a1.istitle())#False
print(a2.istitle())#True
print(a3.istitle())#True
print(a4.istitle())#False
print(a5.istitle())#True

判断字符串是否都是大写或者都是小写
a="A"
a1="AAss"
a2="A1234"
a3="abggg"
判断是否全部为小写
print(a.islower())#False
print(a1.islower())#False
print(a2.islower())#False
print(a3.islower())#True

a="A"
a1="AAss"
a2="A1234"
a3="abggg"
判断是否全部为大写
print(a.isupper())#True
print(a1.isupper())#False
print(a2.isupper())#True
print(a3.isupper())#False

列表
[]中括号来定义
类型:list
a =[]#空列表
print(type(a))#<class 'list'>

a1 =["123"]#列表中这个123 只算是一个元素 索引值为0
print(a1[0])#123

#下列元素中:hello 算一个元素,后面索引值依次类推
a =["hello",1,2,3,4,5,6]#定义一个列表
print(a[5])#5
print(a)#['hello', 1, 2, 3, 4, 5, 6]


通过list类型把字符串转换为列表
a ="hello" #定义字符串
print(type(a))#<class 'str'>
c =list(a) #通过列表的类型把字符串变为列表
print(type(c))#<class 'list'>

a =list('hello')
print(type(a))#<class 'list'>
print(a)#['h', 'e', 'l', 'l', 'o']

列表中常用的函数
通过索引替换
a =[1,2,3,4,5,6]#定义一个列表
a[2]="xiaoliu"#通过索引把对应索引值的进行替换
print(a)#[1, 2, 'xiaoliu', 4, 5, 6]

往列表中添加元素
a =[1,2,3,4,5,6]#定义一个列表
a.append("hello")#添加元素
print(a)#[1, 2, 3, 4, 5, 6, 'hello']

往列表中给指定索引加值
a =[1,2,3,4,5,6]#定义一个列表
a.insert(2,"hello")#指定索引位添加元素
print(a)#[1, 2, 'hello', 3, 4, 5, 6]

移除列表中的元素
a =[1,2,3,4,5,6]#定义一个列表
a.remove(1)#把列表中1 这个元素进行移除
print(a)#[2, 3, 4, 5, 6]

删除索引值对应的值
a =[1,2,3,4,5,6]#定义一个列表
del a[2]#通过del 删除索引值对应的值
print(a)#[1, 2, 4, 5, 6]

通过元素查看对应的索引值
a =[1,2,3,4,5,6]#定义一个列表
print(a.index(5))#通过index查列表中元素的索引值

列表排序
sort 升序排序
sorted 升、降序排序

a =[6,2,8,1,5,4,7,3]#定义一个列表
a.sort()#对当前这个列表进行升序排序
print(a)#[1, 2, 3, 4, 5, 6, 7, 8]

a =[6,2,8,1,5,4,7,3]#定义一个列表
从大到小 true
从小到到falas
print(sorted(a,reverse=True)) #实现降序排序从大到小[8, 7, 6, 5, 4, 3, 2, 1]
print(sorted(a,reverse=False))#实现升序排序从小到大[1, 2, 3, 4, 5, 6, 7, 8]

重点:对列表实现反转输出
a = [1,2,3,4,5,6]#定义个列表
a.reverse()#对列表进行反转
print(a)#[6, 5, 4, 3, 2, 1]

查看删除后剩余的值
查看删除后剩余的值
a =[1,2,3,4,5,6]#定义一个列表
a.pop()#默认删除末尾的值
print(a)#[1, 2, 3, 4, 5]

查看删除的值
a =[1,2,3,4,5,6]#定义一个列表
print(a.pop())#6 直接打印显示被删除的值

通过索引值删除对应的值
a =[1,2,3,4,5,6]#定义一个列表
a.pop(2)#通过索引来删除对应的值
print(a)#[1, 2, 4, 5, 6]

列表的拼接
a =[1,2,3]#定义一个列表
a1=[4,5,6]#定义一个列表
a.extend(a1)#将a1列表与a列表进行拼接
print(a)#[1, 2, 3, 4, 5, 6]


分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册