可变序列和不可变序列的通用操作
- 判断值是否属于序列:
in / not in
m = list(range(10)) # [0,1,2,3,4,5,6,7,8,9,10]
2 in m # True
20 in m # False
name = "apple"
"m" in name # False
"a" in name # True
- 序列链接与重复:"
+
" "*
"
m = [1,2,3] + ["a","b","c"] # [1,2,3,'a','b','c']
x = [1,2,3,4]
y = list(range(3))
z = x + y # [1,2,3,4,0,1,2]
x*3 # [1,2,3,4,1,2,3,4,1,2,3,4]
- 下标索引及切片
x = [1,2,3,4,5,6,7,8]
下标索引:x[1],x[2],x[-1]
切片:x[:5],x[2:5],x[3:],x[:-1]
x = [0,1,2,3,4,5,6,7,8]
x[1] # 1
x[2] # 2
x[-1] # 7
x[:5] # [0,1,2,3,4]
x[2:5] # [2,3,4]
x[3:] # [3,4,5,6,7,8]
x[:-1] # [0,1,2,3,4,5,6,7]
按照一定步长访问数据
List[i:j:n]
n为步长
x = [1,2,3,4,5,6,7,8]
x[0:5:2]
结果为[1,3,5,7]
x[::2]
结果为[0,2,4,6]
基本的内置全局函数
x = list(range(10))
len(x) = 10
max(x) = 9
min(x) = 0
sum(x) = 45
x.index("4")
确定4这个值的下标值
y = [3,3,4,5,5,6,6,6,7,8,9]
x.count("5") = 2
计算出次数