Python3 数据结构

列表进阶用法、栈与队列、列表推导式、元组、集合、字典与遍历技巧。

Beginner
Key Points栈用 list.append/pop,队列用 collections.deque;推导式替代手写循环;集合去重求交并,字典保持插入顺序。

列表

The list is the workhorse mutable sequence. Its methods cover the whole CRUD surface: count, index, insert, remove, reverse, sort. All mutate in place and return None.

Python
a = [66.25, 333, -1, 333, 1, 1234.5]

print(a.count(333), a.count(66.25), a.count('x'))

a.insert(2, -1)
a.append(333)
print(a)
print(a.index(333))

a.remove(333)
print(a)

a.reverse()
print(a)

a.sort()
print(a)
Output
2 1 0
[66.25, 333, -1, -1, 333, 1, 1234.5, 333]
1
[66.25, -1, -1, 333, 1, 1234.5, 333]
[333, 1234.5, 1, 333, -1, -1, 66.25]
[-1, -1, 1, 66.25, 333, 333, 1234.5]

将列表当做栈使用

A stack is last-in-first-out. Use append() to push and pop() to pop — both act on the tail in O(1), so a plain list already is a stack.

Python
stack = [3, 4, 5]

stack.append(6)
stack.append(7)
print(stack)

print(stack.pop())
print(stack)

print(stack.pop())
print(stack.pop())
print(stack)
Output
[3, 4, 5, 6, 7]
7
[3, 4, 5, 6]
6
5
[3, 4]

将列表当作队列使用

A queue is first-in-first-out. pop(0) / insert(0, x) on a list is O(n) because every element shifts; use collections.deque with append / popleft for O(1) at both ends.

Python
from collections import deque

queue = deque(["Eric", "John", "Michael"])
queue.append("Terry")
queue.append("Graham")

print(queue.popleft())
print(queue.popleft())
print(queue)
Output
Eric
John
deque(['Michael', 'Terry', 'Graham'])
For a bounded queue use deque(maxlen=n) — the oldest item is dropped automatically once the limit is reached.

列表推导式

A list comprehension replaces "empty list + loop + append" with [expr for var in iterable if cond]. It supports multiple nested for clauses and several conditions.

Python
squares = []
for x in range(10):
    squares.append(x ** 2)
print(squares)

squares = [x ** 2 for x in range(10)]
print(squares)

print([(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y])
print([-x for x in range(5)])
print([x for x in range(20) if x % 3 == 0])
Output
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
[0, -1, -2, -3, -4]
[0, 3, 6, 9, 12, 15, 18]

嵌套列表解析

A 2-D list can be transposed by iterating rows and picking columns. In [[row[i] for row in matrix] for i in range(4)] the outer comprehension produces the final list and the inner one builds each row.

Python
matrix = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
]

print([[row[i] for row in matrix] for i in range(4)])
print(list(zip(*matrix)))
Output
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

del 语句

del removes by position, can delete a slice, empty the whole list, or even unbind the name itself. It is a statement, not a method.

Python
a = [-1, 1, 66.25, 333, 333, 1234.5]

del a[0]
print(a)

del a[2:4]
print(a)

del a[:]
print(a)
Output
[1, 66.25, 333, 333, 1234.5]
[1, 66.25, 1234.5]
[]

元组和序列

A tuple is defined by commas and is immutable — parentheses are just cosmetic. It can hold mixed types, supports unpacking, and being immutable it may serve as a dict key or set member.

Python
t = 12345, 54321, 'hello!'
print(t[0])
print(t)

u = t, (1, 2, 3, 4, 5)
print(u)

x, y, z = t
print(x, y, z)

s = {t}
print(s)
Output
12345
(12345, 54321, 'hello!')
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
12345 54321 hello!
{(12345, 54321, 'hello!')}

集合

A set is an unordered collection of unique items, built with {} or set(). It supports - difference, | union, & intersection and ^ symmetric difference — ideal for dedup and set algebra.

Python
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(sorted(basket))

print('orange' in basket, 'crabgrass' in basket)

a = set('abracadabra')
b = set('alacazam')
print(sorted(a))
print(sorted(a - b))
print(sorted(a | b))
print(sorted(a & b))
print(sorted(a ^ b))
Output
['apple', 'banana', 'orange', 'pear']
True False
['a', 'b', 'c', 'd', 'r']
['b', 'd', 'r']
['a', 'b', 'c', 'd', 'l', 'm', 'r', 'z']
['a', 'c']
['b', 'd', 'l', 'm', 'r', 'z']
An empty set is set(), not {} — that is an empty dict. Set members must be hashable: lists are out, tuples are fine.

字典

A dict maps keys to values via {key: value}. Keys must be hashable and unique; values are unrestricted. Since 3.7 iteration follows insertion order, and in tests keys.

Python
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
print(tel)

print(tel['jack'])

del tel['sape']
tel['irv'] = 4127
print(tel)
print(list(tel))
print(sorted(tel))

print('guido' in tel, 'jack' not in tel)

print(dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]))
print({x: x ** 2 for x in (2, 4, 6)})
print(dict(sape=4139, guido=4127, jack=4098))
Output
{'jack': 4098, 'sape': 4139, 'guido': 4127}
4098
{'jack': 4098, 'guido': 4127, 'irv': 4127}
['jack', 'guido', 'irv']
['guido', 'irv', 'jack']
True False
{'sape': 4139, 'guido': 4127, 'jack': 4098}
{2: 4, 4: 16, 6: 36}
{'sape': 4139, 'guido': 4127, 'jack': 4098}

遍历技巧

Use items() for key/value pairs, enumerate() for indices, zip() to walk sequences in parallel, reversed() to go backwards, and sorted(set(...)) for deduplicated ordered output.

Python
knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for k, v in knights.items():
    print(k, v)

for i, v in enumerate(['tic', 'tac', 'toe']):
    print(i, v)

questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip(questions, answers):
    print('What is your {0}?  It is {1}.'.format(q, a))

for i in reversed(range(1, 10, 2)):
    print(i, end=' ')
print()

basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
for f in sorted(set(basket)):
    print(f)
Output
gallahad the pure
robin the brave
0 tic
1 tac
2 toe
What is your name?  It is lancelot.
What is your quest?  It is the holy grail.
What is your favorite color?  It is blue.
9 7 5 3 1 
apple
banana
orange
pear

参阅文档

That covers the vast majority of everyday data-structure work. For the full definitions and edge cases see the official docs on built-in types, the collections module (deque, Counter, defaultdict, namedtuple) and specialised containers such as heapq, bisect, array.

  • Built-in Types — list, tuple, set, dict
  • collections — deque, Counter, defaultdict, namedtuple
  • heapq / bisect — priority queues and binary search
  • array / struct — compact numeric storage

Friends & Partners