count计算元素出现的次数
t = [‘to‘,‘be‘,‘or‘,‘not‘,‘to‘,‘be‘].count(‘to‘) print(t)
输出结果:2
extend用法
a = [1,2,3] b = [4,5,6] a.extend(b) print(a) print(b)
输出结果:
index查找位置,只能索引一个
a = [‘wuchao‘,‘jinxin‘,‘xiaohu‘,‘sanpang‘,‘liqang‘] print(a.index(‘jinxin‘))
输出结果:1
取第二个李刚,尼玛,要仔细看看了...
# index 根据内容找位置
reverse
a = [‘wuchao‘,‘jinxin‘,‘ligang‘,‘xiaohu‘,‘sanpang‘,‘liqang‘] a.reverse() print(a)
sorted排序
x = [4,5,2,1,9,8] x.sort() print(x)
输出结果:[1, 2, 4, 5, 8, 9]
字符串排序,按照ASSCII码排序
a = [‘wuchao‘,‘jinxin‘,‘ligang‘,‘xiaohu‘,‘sanpang‘,‘liqang‘] a.sort() print(a)
输出结果:[‘jinxin‘, ‘ligang‘, ‘liqang‘, ‘sanpang‘, ‘wuchao‘, ‘xiaohu‘]
从大到小排列
x = [4,9,1,3,1,7,9,12,78,1] x.sort(reverse=True) print(x)
判断haidilaoge是否在列表中
a = [‘wuchao‘,‘jinxin‘,‘ligang‘,‘xiaohu‘,‘sanpang‘,‘liqang‘] print(a.count("haidilaoge"))
输出结果:0
方法2:
a = [‘wuchao‘,‘jinxin‘,‘ligang‘,‘xiaohu‘,‘sanpang‘,‘liqang‘] print("haidilao ge" in a)
输出结果:False
总结:
a.clear() 清空
时间: 2024-10-20 10:34:36