Python3 列表

列表的访问、更新、删除、脚本操作符、截取拼接、嵌套与常用函数方法。

Beginner
Key Points列表是可变有序序列,下标从 0 开始、支持负索引与切片;增删用 append/insert/pop/remove/del,排序用 sort。

访问列表中的值

A list is defined with square brackets and comma-separated items of any type. Use an index for one element and a slice [start:stop:step] for a sub-list — start inclusive, stop exclusive.

Python
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7]

print("list1[0]:", list1[0])
print("list1[-1]:", list1[-1])
print("list2[1:5]:", list2[1:5])
print("list2[:3]:", list2[:3])
Output
list1[0]: physics
list1[-1]: 2000
list2[1:5]: [2, 3, 4, 5]
list2[:3]: [1, 2, 3]

更新列表

Lists are mutable: assign to an index to replace one item, or assign to a slice to replace a whole range at once (the length may differ).

Python
list1 = ['physics', 'chemistry', 1997, 2000]
print("before:", list1[2])

list1[2] = 2001
print("after :", list1[2])
print(list1)

list1[1:3] = ['math', 2024]
print(list1)
Output
before: 1997
after : 2001
['physics', 'chemistry', 2001, 2000]
['physics', 'math', 2024, 2000]

删除列表元素

Three common ways to delete: del by position (slices too), remove() deletes the first matching value, pop() removes by position and returns the item.

Python
list1 = ['physics', 'chemistry', 1997, 2000]
print("before:", list1)

del list1[2]
print("del list1[2]:", list1)

list1.remove('chemistry')
print("remove:", list1)

popped = list1.pop()
print("pop:", popped, "->", list1)
Output
before: ['physics', 'chemistry', 1997, 2000]
del list1[2]: ['physics', 'chemistry', 2000]
remove: ['physics', 2000]
pop: 2000 -> ['physics']

Python 列表脚本操作符

Lists support + concatenation, * repetition, in membership, len(), and for ... in iteration. + returns a new list rather than mutating in place.

ExpressionResultDescription
len([1, 2, 3])3Length
[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]Concatenation
['Hi!'] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']Repetition
3 in [1, 2, 3]TrueMembership
for x in [1, 2, 3]: print(x)1 2 3Iteration
Python
print(len([1, 2, 3]))
print([1, 2, 3] + [4, 5, 6])
print(['Hi!'] * 4)
print(3 in [1, 2, 3])

for x in [1, 2, 3]:
    print(x, end=' ')
print()
Output
3
[1, 2, 3, 4, 5, 6]
['Hi!', 'Hi!', 'Hi!', 'Hi!']
True
1 2 3

Python 列表截取与拼接

Slicing extracts parts: L[2] is the third item, L[-2] the second from the end, L[1:] everything from the second item. Concatenate with += or +.

Python
L = ['Google', 'Runoob', 'Taobao']

print(L[2])
print(L[-2])
print(L[1:])

L += ['Baidu']
print(L)
print(len(L))
Output
Taobao
Runoob
['Runoob', 'Taobao']
['Google', 'Runoob', 'Taobao', 'Baidu']
4

嵌套列表

List items can themselves be lists, giving 2-D or higher-dimensional structures. Chain indices like x[0][1] to descend level by level.

Python
a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]

print(x)
print(x[0])
print(x[0][1])
print(x[1][2])
Output
[['a', 'b', 'c'], [1, 2, 3]]
['a', 'b', 'c']
b
3

列表比较

Lists compare lexicographically element by element. == tests full equality; the operator module offers the same comparisons as functions.

Python
import operator

a = [1, 2]
b = [2, 3]

print(a == b, a != b)
print(a < b, a > b)
print(operator.eq(a, b), operator.lt(a, b))
print([1, 2] == [1, 2], [1, 2] is [1, 2])
Output
False True
True False
False True
True False

Python 列表函数 &amp; 方法

Built-ins len/max/min/sum work on lists; methods append/insert/extend/sort/reverse/count/index/remove/pop/clear mutate in place. sort() returns None — the order lives in the list itself.

Python
nums = [3, 1, 4, 1, 5]

print(len(nums), max(nums), min(nums), sum(nums))

nums.append(9)
nums.insert(0, 0)
print(nums)

nums.sort()
print(nums)

nums.reverse()
print(nums)
print(nums.count(1), nums.index(4))
Output
5 5 1 14
[0, 3, 1, 4, 1, 5, 9]
[0, 1, 1, 3, 4, 5, 9]
[9, 5, 4, 3, 1, 1, 0]
2 2
sort() sorts in place and returns None. Use sorted(nums) for a new list, and reverse=True for descending order.

Friends & Partners