namedtuple
- 用于创建带有命名字段的元组(named tuple)。它的主要目的是为了让您创建更具可读性和可维护性的数据结构,类似于使用类定义的对象,但比普通元组更轻量。
namedtuple
的基本语法如下:from collections import namedtuple MyTuple = namedtuple('MyTuple', ['field1', 'field2', ...])
'MyTuple'
是元组类型的名称,您可以自定义它。['field1', 'field2', ...]
是元组的字段名称列表。from collections import namedtuple # 创建一个名为 'Person' 的命名元组类型,有 'name' 和 'age' 两个字段 Person = namedtuple('Person', ['name', 'age']) # 创建一个 'Person' 的实例 person1 = Person(name='Alice', age=30) # 访问字段值 print(person1.name) # 输出: 'Alice' print(person1.age) # 输出: 30 # 创建另一个实例 person2 = Person(name='Bob', age=25)
deque
deque
(发音为 “deck”)是 Python 中collections
模块中的一种数据结构,它代表了一个双向队列(double-ended queue),可以高效地从队列的两端添加或删除元素。deque
提供了与列表(list
)相似的功能,但在特定情况下,它的性能更好。创建一个
deque
对象:my_deque = deque()
向队列的一端添加元素:
my_deque.append(1) # 从右端添加元素 my_deque.appendleft(2) # 从左端添加元素
查看队列的内容和长度:
print(my_deque) # 打印队列的内容 print(len(my_deque)) # 打印队列的长度
在队列的两端执行其他操作:
my_deque.extend([3, 4, 5]) # 从右端扩展队列 my_deque.extendleft([0, -1, -2]) # 从左端扩展队列
在队列中查找元素:
index = my_deque.index(3) # 查找元素 3 的索引 count = my_deque.count(2) # 计算元素 2 的出现次数
Counter
Counter
是 Python 中collections
模块中的一个类,用于快速计数可迭代对象中元素的出现次数。Counter
返回一个字典,其中键是可迭代对象中的元素,而值是该元素在可迭代对象中出现的次数。# 创建一个计数对象 my_list = [1, 2, 3, 1, 2, 3, 4, 1, 2, 1] counter = Counter(my_list) # 计算元素的出现次数 count_1 = counter[1] # 元素 1 出现 4 次 count_2 = counter[2] # 元素 2 出现 3 次 count_3 = counter[3] # 元素 3 出现 2 次 count_4 = counter[4] # 元素 4 出现 1 次 # 查看计数对象的内容 print(counter) # 输出: Counter({1: 4, 2: 3, 3: 2, 4: 1}) # 获取所有元素及其出现次数 element_counts = counter.items() # 输出: dict_items([(1, 4), (2, 3), (3, 2), (4, 1)]) # 获取最常见的元素 most_common = counter.most_common(2) # 获取出现次数最多的前两个元素 # 输出: [(1, 4), (2, 3)]
itertools
itertools
是 Python 中的一个模块,提供了一组用于创建和操作迭代器的工具函数。这些函数能够帮助您在处理迭代对象(如列表、元组、集合等)时更灵活和高效地生成、过滤和组合元素。count(start=0, step=1)
: 创建一个无限迭代器,从start
开始,以step
为步长生成整数。from itertools import count for i in count(10, 2): if i > 20: break print(i)
cycle(iterable)
: 无限迭代地重复iterable
中的元素。from itertools import cycle my_list = [1, 2, 3] for num in cycle(my_list): if num > 5: break print(num)
repeat(elem, n)
: 生成一个无限迭代器,不断重复elem
元素n
次。from itertools import repeat for i in repeat('Hello', 3): print(i)
combinations(iterable, r)
: 返回iterable
中长度为r
的所有可能组合,不重复。from itertools import combinations my_list = [1, 2, 3] combs = list(combinations(my_list, 2)) print(combs) # 输出: [(1, 2), (1, 3), (2, 3)]
permutations(iterable, r)
: 返回iterable
中长度为r
的所有可能排列,不重复。from itertools import permutations my_list = [1, 2, 3] perms = list(permutations(my_list, 2)) print(perms) # 输出: [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
filterfalse(predicate, iterable)
: 返回iterable
中不满足predicate
函数条件的元素。from itertools import filterfalse my_list = [1, 2, 3, 4, 5] filtered = list(filterfalse(lambda x: x % 2 == 0, my_list)) print(filtered) # 输出: [1, 3, 5]
chain(iterable1, iterable2, ...)
: 将多个可迭代对象连接成一个单一的迭代器。from itertools import chain list1 = [1, 2, 3] list2 = ['a', 'b', 'c'] combined = list(chain(list1, list2)) print(combined) # 输出: [1, 2, 3, 'a', 'b', 'c']