列表通用操作
Posted 炎之朱雀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了列表通用操作相关的知识,希望对你有一定的参考价值。
---以下部分截取自雨辰的教学
x = [1,2,3] name = list("天下为公") print(range(5)) print(list(range(5))) print(x) print(name) names = ["Tom" , "Jerry" , "Mike" , "Peter" , "John"] scores = [99 , 88 ,76.3 , 90.5 , 77] print("Mike" in names) #判定对象Mike是在再names序列里面 print("mike" in names) print("mike" not in names) #判定对象mike是否不在names序列里面 print(88 in scores) print(101 in scores) print(101 not in scores) x = list(range(4)) y = [98 , 99 , 100] print(x + y) print(x) print([3] * 4) print(names[0]) print(scores[-1]) print(len(scores))#对象个数 #类型必须可以比较 print(min(scores))#最小值 print(max(scores))#最大值 print(sum(scores))#总和 cities = ["北京" ,"上海" , "广州" , "深圳" , "天津"] #索引位置在两个值之间 print(cities[0:2]) print(cities[0:3]) print(cities[-5:-3]) print(cities[:3]) print(cities[1:]) print(cities[:]) #第二个冒号后面的是步长,默认是1步 print(cities[::2]) print(len(cities)) print(cities.index("上海"))#索引位置 print(scores.index(77)) print(cities.count("天津")) #出现次数 print(scores.count(77)) #出现次数 l = [1,2,3,4,2,3,3,3,2] print(l.count(3))
x = list(range(1, 11)) print(x) x[0] = 99 # 列表支持原位改变某一个元素值 print(x) x[:3] =[98,99,100] print(x) x[:3] =[98,99,100,101,102] print(x) x[:5] =[99] #所赋的值必须是可迭代的对象 print(x) l = list(range(1,11)) print(l) l[::2] = [99,99,99,99,99] #所赋的值不但要可迭代,还有数目相匹配 print(l) del l[0] print(l) del l[:3] print(l) del l[::2] print(l) l[:2] = [] print(l) s = [1,2,3,4,5,6,7,8,2,3,2,2] print(s) s.remove(2) #.remove只删除第一个匹配的值 print(s) s.clear() print(s) s.append(5) print(s) s.append(6) print(s) s.append([7,8]) print(s) #追加的对象视为一个值 s.extend([9,10]) #扩展一个序列 print(s) s.insert(2,7) print(s) s[3:3] = [8] #不同手段,达到同样目的 print(s)
s = [1,2,3,4,5,6,7,8,2,3,2,2] print(s)
print(s.pop()) result = s.pop() print(result) print(s.pop(-1)) print(s.reverse()) print(s) x = s.reverse() print(x) #reverse只反转当前对象,不返回值 l = list(range(11)) s = l[:] x = l.copy() #只复制值,不共享索引位置 x[0] = 98 s[0] = 99 print(s) print(x) print(l)
以上是关于列表通用操作的主要内容,如果未能解决你的问题,请参考以下文章