Python Sequence Types

三种基础的 sequence Types:lists, tuples, range

types class literal mutable
lists list [] yes
tuples tuple () no
ranges range   no

常见操作

以 list 为例

a = ['a', 'b', 'c']
len(a)  # 3

索引为负数时表示从后往前

a[0]   # 'a'
a[-1]  # 'c'
a[100] # IndexError

是否包含

if 'b' in a:
    pass

if 'd' not in a:
    pass

Slice

切片 [start:end]

a[1:2]   # ['b']
a[1:]    # ['b', 'c']
a[:-1]   # ['a', 'b']
a[:]     # ['a', 'b', 'c']

给切片赋值表示替换

a[1:2] = [1, 2, 3] # a 变成 ['a', 1, 2, 3, 'c']
a[:] = [] # 清空 a

Looping

a = ['a', 'b', 'c']

for v in a:
    print(v)

for i, v in enumerate(a):
    print(i, v)

# 同时遍历多个序列
b = [1, 2]
for x, y in zip(a, b):
    print(x, y)

Concat

a = ['a', 'b', 'c']
b = [1, 2]
a + b
# ['a', 'b', 'c', 1, 2]