Python常见的数据结构
list
、deque
、set
和dict
四种数据结构
列表list 时间复杂度
Python列表 (list
) 中一些常见操作的具体API用法及时间复杂度说明:
1. Copy (复制)
- API:
new_list = old_list.copy()
- Description: 创建一个列表的浅拷贝。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
2. Append (追加)
- API:
lst.append(item)
- Description: 向列表末尾添加一个元素。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(1)
- Average Case:
3. Pop (弹出)
- API:
item = lst.pop([index])
- Description: 移除列表末尾的元素并返回它;如果提供了索引,则移除该位置的元素并返回。
- Time Complexity:
- Pop last:
O(1)
- Pop intermediate:
O(n)
- Pop last:
4. Insert (插入)
- API:
lst.insert(index, item)
- Description: 在指定位置插入一个元素。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
5. Get Item (获取元素)
- API:
item = lst[index]
- Description: 获取列表中指定索引处的元素。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(1)
- Average Case:
6. Set Item (设置元素)
- API:
lst[index] = item
- Description: 修改列表中指定索引处的元素。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(1)
- Average Case:
7. Delete Item (删除元素)
- API:
del lst[index]
- Description: 删除列表中指定索引处的元素。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
8. Iteration (迭代)
- API:
for item in lst: ...
- Description: 遍历列表中的每个元素。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
9. Get Slice (获取切片)
- API:
slice = lst[start:end:step]
- Description: 获取列表的一部分。
- Time Complexity:
- Average Case:
O(k)
- Amortized Worst Case:
O(k)
- Average Case:
10. Del Slice (删除切片)
- API:
del lst[start:end:step]
- Description: 删除列表的一部分。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
11. Set Slice (设置切片)
- API:
lst[start:end:step] = other_sequence
- Description: 替换列表的一部分。
- Time Complexity:
- Average Case:
O(k + n)
- Amortized Worst Case:
O(k + n)
- Average Case:
12. Extend (扩展)
- API:
lst.extend(other_sequence)
- Description: 将另一个序列的元素添加到列表末尾。
- Time Complexity:
- Average Case:
O(k)
- Amortized Worst Case:
O(k)
- Average Case:
13. Sort (排序)
- API:
lst.sort(key=None, reverse=False)
- Description: 就地排序列表。
- Time Complexity:
- Average Case:
O(n log n)
- Amortized Worst Case:
O(n log n)
- Average Case:
14. Multiply (重复)
- API:
new_list = lst * k
- Description: 重复列表
k
次。 - Time Complexity:
- Average Case:
O(nk)
- Amortized Worst Case:
O(nk)
- Average Case:
15. Membership Test (成员测试)
- API:
item in lst
- Description: 检查元素是否存在于列表中。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
16. Min/Max (最小/最大值)
- API:
min_val = min(lst)
,max_val = max(lst)
- Description: 找到列表中的最小值或最大值。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
17. Get Length (获取长度)
- API:
length = len(lst)
- Description: 获取列表的长度。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(1)
- Average Case:
Operation | Average Case | Amortized Worst Case |
---|---|---|
Copy | O(n) | O(n) |
Append | O(1) | O(1) |
Pop last | O(1) | O(1) |
Pop intermediate | O(n) | O(n) |
Insert | O(n) | O(n) |
Get Item | O(1) | O(1) |
Set Item | O(1) | O(1) |
Delete Item | O(n) | O(n) |
Iteration | O(n) | O(n) |
Get Slice | O(k) | O(k) |
Del Slice | O(n) | O(n) |
Set Slice | O(k + n) | O(k + n) |
Extend | O(k) | O(k) |
Sort | O(n log n) | O(n log n) |
Multiply | O(nk) | O(nk) |
x in s | O(n) | O(n) |
min(s), max(s) | O(n) | O(n) |
Get Length | O(1) | O(1) |
这里,“n”代表列表中的元素总数,“k”代表操作影响的元素数量。例如,在切片操作中,“k”是切片长度。对于append
和extend
操作,由于列表在内部可能需要重新分配内存来容纳更多的元素,因此在某些情况下这些操作可能需要更多的时间,但是从长远来看,它们的时间复杂度被摊销为O(1)。
collections.deque 时间复杂度
collections.deque
(双端队列)的一些常见操作的具体API用法及其时间复杂度说明:
1. Copy (复制)
- API:
new_deque = old_deque.copy()
- Description: 创建一个deque的浅拷贝。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
2. append (追加)
- API:
dq.append(item)
- Description: 在deque的右侧追加一个元素。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(1)
- Average Case:
3. appendleft (左侧追加)
- API:
dq.appendleft(item)
- Description: 在deque的左侧追加一个元素。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(1)
- Average Case:
4. pop (弹出)
- API:
item = dq.pop()
- Description: 移除deque右侧的元素并返回它。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(1)
- Average Case:
5. popleft (左侧弹出)
- API:
item = dq.popleft()
- Description: 移除deque左侧的元素并返回它。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(1)
- Average Case:
6. extend (扩展)
- API:
dq.extend(iterable)
- Description: 从右侧将iterable中的元素依次追加到deque。
- Time Complexity:
- Average Case:
O(k)
- Amortized Worst Case:
O(k)
- Average Case:
7. extendleft (左侧扩展)
- API:
dq.extendleft(iterable)
- Description: 从左侧将iterable中的元素逆序追加到deque。
- Time Complexity:
- Average Case:
O(k)
- Amortized Worst Case:
O(k)
- Average Case:
8. rotate (旋转)
- API:
dq.rotate(n=1)
- Description: 将deque中的元素向右旋转n次。如果n为负数,则向左旋转。
- Time Complexity:
- Average Case:
O(k)
- Amortized Worst Case:
O(k)
- Average Case:
9. remove (移除)
- API:
dq.remove(value)
- Description: 从deque中移除第一个出现的value。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
10. Get Length (获取长度)
- API:
length = len(dq)
- Description: 获取deque的长度。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(1)
- Average Case:
deque的设计旨在提供高效的两端操作,因此append
, appendleft
, pop
, popleft
等操作都是常数时间复杂度。而remove
操作则需要线性时间,因为它需要遍历deque以找到要移除的第一个匹配项。
Operation | Average Case | Amortized Worst Case |
---|---|---|
Copy | O(n) | O(n) |
append | O(1) | O(1) |
appendleft | O(1) | O(1) |
pop | O(1) | O(1) |
popleft | O(1) | O(1) |
extend | O(k) | O(k) |
extendleft | O(k) | O(k) |
rotate | O(k) | O(k) |
remove | O(n) | O(n) |
Get Length | O(1) | O(1) |
集合set 时间复杂度
Python set
(集合)的一些常见操作的具体API用法及其时间复杂度说明:
1. Membership Test (成员测试)
- API:
item in s
- Description: 检查元素是否存在于集合中。
- Time Complexity:
- Average Case:
O(1)
- Worst Case:
O(n)
- Average Case:
2. Union (并集)
- API:
union_set = s.union(t)
- Description: 返回一个新的集合,包含两个集合中的所有不同元素。
- Time Complexity:
- Average Case:
O(len(s) + len(t))
- Worst Case:
O(len(s) + len(t))
- Average Case:
3. Intersection (交集)
- API:
intersection_set = s.intersection(t)
- Description: 返回一个新的集合,包含两个集合中共有的元素。
- Time Complexity:
- Average Case:
O(min(len(s), len(t)))
- Worst Case:
O(len(s) * len(t))
- Notes: 如果
t
不是集合,则时间复杂度为O(max(len(s), len(t)))
.
- Average Case:
4. Multiple Intersection (多个集合交集)
- API:
multiple_intersection_set = s1.intersection(s2, ..., sn)
- Description: 返回一个新的集合,包含多个集合中共有的元素。
- Time Complexity:
- Average Case:
(n-1) * O(l)
其中l
是max(len(s1), ..., len(sn))
- Worst Case: 不适用(实际情况下,取决于实现细节)
- Average Case:
5. Difference (差集)
- API:
difference_set = s.difference(t)
- Description: 返回一个新的集合,包含
s
中但不在t
中的元素。 - Time Complexity:
- Average Case:
O(len(s))
- Worst Case:
O(len(s))
- Average Case:
6. Difference Update (差集更新)
- API:
s.difference_update(t)
- Description: 就地修改
s
,移除与t
中相同的元素。 - Time Complexity:
- Average Case:
O(len(t))
- Worst Case:
O(len(s) * len(t))
- Average Case:
7. Symmetric Difference (对称差集)
- API:
symmetric_difference_set = s.symmetric_difference(t)
- Description: 返回一个新的集合,包含在
s
或t
中但不在两者都出现的元素。 - Time Complexity:
- Average Case:
O(len(s))
- Worst Case:
O(len(s) * len(t))
- Average Case:
8. Symmetric Difference Update (对称差集更新)
- API:
s.symmetric_difference_update(t)
- Description: 就地修改
s
,使其成为s
和t
的对称差集。 - Time Complexity:
- Average Case:
O(len(t))
- Worst Case:
O(len(t) * len(s))
- Average Case:
集合的实现通常基于哈希表,这意味着大多数操作的时间复杂度在平均情况下接近常数时间,但在最坏的情况下可能会退化为线性时间。特别是当哈希冲突发生时,某些操作的时间复杂度可能会增加。
Operation | Average case | Worst Case | notes |
---|---|---|---|
x in s | O(1) | O(n) | |
Union s&t | O(len(s)+len(t)) | ||
Intersection s&t | O(min(len(s), len(t))) | O(len(s) * len(t)) | replace “min” with “max” if t is not a set |
Multiple intersection s1&s2&…&sn | (n-1)*O(l where l is max(len(s1),…len(sn)) | ||
Difference s-t | O(len(s)) | ||
s.difference_update(t) | O(len(t)) | O(len(s) * len(t)) | |
Symmetric Difference s^t | O(len(s)) | O(len(s) * len(t)) | |
s.symmetric_difference_update(t) | O(len(t)) | O(len(t) * len(s)) |
字典dict 时间复杂度
Python dict
(字典)的一些常见操作的具体API用法及其时间复杂度说明:
1. Membership Test (成员测试)
- API:
key in d
- Description: 检查键是否存在于字典中。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(n)
- Average Case:
2. Copy (复制)
- API:
new_dict = old_dict.copy()
- Description: 创建一个字典的浅拷贝。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
3. Get Item (获取元素)
- API:
value = d[key]
- Description: 根据键获取对应的值。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(n)
- Average Case:
4. Set Item (设置元素)
- API:
d[key] = value
- Description: 添加键值对或更新已存在的键的值。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(n)
- Average Case:
5. Delete Item (删除元素)
- API:
del d[key]
- Description: 删除字典中指定键的条目。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(n)
- Average Case:
6. Iteration (迭代)
- API:
for key in d: ...
- Description: 遍历字典中的每个键。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
7. Additional Operations (其他操作)
Get Method (安全获取元素)
- API:
value = d.get(key, default)
- Description: 获取字典中键对应的值,如果键不存在则返回默认值。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(n)
- Average Case:
Items and Keys (获取键值对和键)
- API:
items = d.items()
,keys = d.keys()
- Description: 获取字典中的键值对或键的视图。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
Values (获取值)
- API:
values = d.values()
- Description: 获取字典中所有的值的视图。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
Pop (弹出)
- API:
value = d.pop(key, default)
- Description: 移除并返回字典中指定键的值,如果键不存在则返回默认值。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(n)
- Average Case:
Popitem (弹出键值对)
- API:
key_value = d.popitem()
- Description: 移除并返回字典中的任意一个键值对。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(n)
- Average Case:
Clear (清空)
- API:
d.clear()
- Description: 清空字典。
- Time Complexity:
- Average Case:
O(n)
- Amortized Worst Case:
O(n)
- Average Case:
Setdefault (设置默认值)
- API:
value = d.setdefault(key, default)
- Description: 如果键不存在,则使用默认值设置键值对并返回该值;如果键存在,则返回对应的值。
- Time Complexity:
- Average Case:
O(1)
- Amortized Worst Case:
O(n)
- Average Case:
Update (更新)
- API:
d.update(other_dict)
- Description: 将另一个字典的内容合并到当前字典中。
- Time Complexity:
- Average Case:
O(m)
其中m
是other_dict
的长度 - Amortized Worst Case:
O(m * n)
- Average Case:
字典的实现通常基于哈希表,这意味着大多数操作的时间复杂度在平均情况下接近常数时间,但在最坏的情况下可能会退化为线性时间。特别是当哈希冲突发生时,某些操作的时间复杂度可能会增加。
Operation | Average Case | Amortized Worst Case |
---|---|---|
k in d | O(1) | O(n) |
Copy[3] | O(n) | O(n) |
Get Item | O(1) | O(n) |
Set Item[1] | O(1) | O(n) |
Delete Item | O(1) | O(n) |
Iteration[3] | O(n) | O(n) |
(补充)构造方法
1. List (列表)
- API:
list([iterable])
- Description: 创建一个列表。如果提供了可迭代对象,则会从该对象构建列表。
- Example:
# 使用空列表 lst = list() # 使用给定的可迭代对象构建列表 lst = list(range(5)) print(lst) # 输出: [0, 1, 2, 3, 4]
2. Deque (双端队列)
- API:
collections.deque([iterable[, maxlen]])
- Description: 创建一个双端队列。如果提供了可迭代对象,则会从该对象构建deque。
maxlen
参数可选,用于限制deque的最大长度。 - Example:
from collections import deque # 使用空deque dq = deque() # 使用给定的可迭代对象构建deque dq = deque(range(5)) print(dq) # 输出: deque([0, 1, 2, 3, 4]) # 使用最大长度构建deque dq = deque(range(5), maxlen=3) print(dq) # 输出: deque([2, 3, 4], maxlen=3)
3. Set (集合)
- API:
set([iterable])
- Description: 创建一个集合。如果提供了可迭代对象,则会从该对象构建集合。集合中的元素是唯一的。
- Example:
# 使用空集合 s = set() # 使用给定的可迭代对象构建集合 s = set([1, 2, 3, 3, 4, 4, 4]) print(s) # 输出: {1, 2, 3, 4}
4. Dict (字典)
- API:
dict([mapping, **kwargs])
或dict(iterable)
- Description: 创建一个字典。如果提供了映射对象,则会从该对象构建字典。也可以通过关键字参数初始化字典。如果提供了可迭代对象,则假设它是键值对的元组组成的序列。
- Example:
# 使用空字典 d = dict() # 使用关键字参数构建字典 d = dict(a=1, b=2, c=3) print(d) # 输出: {'a': 1, 'b': 2, 'c': 3} # 使用给定的可迭代对象构建字典 d = dict([(1, 'one'), (2, 'two')]) print(d) # 输出: {1: 'one', 2: 'two'}