Python学习笔记014

列表

统计

names=['to','be','or','not','to','be']

#count 统计列表中元素出现次数
to=names.count('to')
print(to)

#extend 一个列表加到另一个列表中
_name_Chinese=['Zhao','Qian','Sun','Li']
names.extend(_name_Chinese)
print(names)
print(_name_Chinese)

#index 获取元素的索引
a=names.index('Zhao')
print(a)

#index获取多个重复值得索引
#获取第一个值的索引
b=names.index('be')
#从第一个值后开始切片
c=names[b+1:]
#获取切片中第二个值的索引
d=c.index('be')
#两个索引值相加再加1
e=b+d+1
print(b,c,d,e)

#reserve 把列表反向排列
c.reverse()
print(c)

#sort 把列表元素进行排序,默认按ASCII码表顺序
x=[1,5,8,6,4,2]
x.sort()
print(x)

c.sort()
print(c)

#sort 带有reverse参数,当reverse=False时,按逆向排序
y=[1,5,8,6,4,2]
y.sort(reverse=True)
print(y)

猜你喜欢

转载自www.cnblogs.com/wtzxxy/p/12389752.html
014
今日推荐