列表特性
空列表
empty = []
列表示例
numbers = [1, 2, 3, 4, 5]
strings = ['apple', 'banana', 'orange']
mixed = [12, 55, 'China', {'age': 25}]
nested = [10, 20, [1, 2, 3, 4]]
访问元素
numbers = [10, 20, 30, 40, 50]
numbers[0] # 10
删除元素
numbers = [10, 20, 30, 40, 50]
del numbers[1] # [10, 30, 40, 50]
列表长度
numbers = [10, 20, 30, 40, 50]
len(numbers) # 5
列表最大值和最小值
numbers = [10, 20, 30, 40, 50]
max(numbers) # 50
min(numbers) # 10
列表遍历
numbers = [10, 20, 30, 40, 50]
for number in numbers:
print(number)
numbers = [10, 20, 30, 40, 50]
for index in range(len(numbers)):
numbers[index] = numbers[index] * 2
列表合并
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b # [1, 2, 3, 4, 5, 6]
列表重复
x = [1, 2, 3] * 2 # [1, 2, 3, 1, 2, 3]
成员检查
1 in [1, 2, 3] # True
列表分片
列表方法