itertools
爲了高效循環而生成各種迭代器的工具
對,裏面每個函數返回的都是一個迭代器對象
1. 無限循環的迭代器
count()
根據初始值和步長永遠增長下去count(start=0, step=1)
- 0 1 2 3 4 …
cycle()
循環輸出傳入的可迭代對象cycle('123')
- 1 2 3 1 2 3 …
repeat()
重複輸入一定次數repeat('ABC', times=None)
- ABC ABC ABC …
2. 排列組合相關的
Iterator | Arguments | Results |
---|---|---|
product() |
p, q, … [repeat=1] | cartesian product, equivalent to a nested for-loop |
permutations() |
p[, r] | r-length tuples, all possible orderings, no repeated elements |
combinations() |
p, r | r-length tuples, in sorted order, no repeated elements |
combinations_with_replacement() |
p, r | r-length tuples, in sorted order, with repeated elements |
product('ABCD', repeat=2) |
AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD | |
permutations('ABCD', 2) |
AB AC AD BA BC BD CA CB CD DA DB DC | |
combinations('ABCD', 2) |
AB AC AD BC BD CD | |
combinations_with_replacement('ABCD', 2) |
AA AB AC AD BB BC BD CC CD DD |
只用位置標記不同對象,而不是真實存放的值。
2.1 product
求多個可迭代對象的笛卡爾積
等價於嵌套的 for 循環
用法
product(*iterables, repeat=1)
repeat
表示前面的多個可迭代對象依照順序一共來幾份
示例
from itertools import product
res = list(
product([1, 2], ['a', 'b'], repeat=2)
)
for i in res:
print(i)
print(u"實際總數:", len(res))
print(u"計算總數:", 2 ** 4)
# (1, 'a', 1, 'a')
# (1, 'a', 1, 'b')
# (1, 'a', 2, 'a')
# (1, 'a', 2, 'b')
# (1, 'b', 1, 'a')
# (1, 'b', 1, 'b')
# (1, 'b', 2, 'a')
# (1, 'b', 2, 'b')
# (2, 'a', 1, 'a')
# (2, 'a', 1, 'b')
# (2, 'a', 2, 'a')
# (2, 'a', 2, 'b')
# (2, 'b', 1, 'a')
# (2, 'b', 1, 'b')
# (2, 'b', 2, 'a')
# (2, 'b', 2, 'b')
# 實際總數: 16
# 計算總數: 16
2.2 permutations
排列 A(n,r)
用法
permutations(iterable, r=None)
r
不填時表示 A(n,n)
示例
from itertools import permutations
res = list(permutations([1, 2, 3, 4], 3))
for i in res:
print(i)
print(u"實際總數:", len(res))
print(u"計算總數:", 4 * 3 * 2)
# (1, 2, 3)
# (1, 2, 4)
# (1, 3, 2)
# (1, 3, 4)
# (1, 4, 2)
# (1, 4, 3)
# (2, 1, 3)
# (2, 1, 4)
# (2, 3, 1)
# (2, 3, 4)
# (2, 4, 1)
# (2, 4, 3)
# (3, 1, 2)
# (3, 1, 4)
# (3, 2, 1)
# (3, 2, 4)
# (3, 4, 1)
# (3, 4, 2)
# (4, 1, 2)
# (4, 1, 3)
# (4, 2, 1)
# (4, 2, 3)
# (4, 3, 1)
# (4, 3, 2)
# 實際總數: 24
# 計算總數: 24
2.3 combinations
組合 C(n,r)
用法
combinations(iterable, r)
r
必填,r 大於 n 時返回空迭代結果
示例
from itertools import combinations
res = list(combinations([1, 2, 3, 4], 3))
for i in res:
print(i)
print(u"實際總數:", len(res))
print(u"計算總數:", 4 * 3 * 2 // (3 * 2 * 1))
# (1, 2, 3)
# (1, 2, 4)
# (1, 3, 4)
# (2, 3, 4)
# 實際總數: 4
# 計算總數: 4
2.4 combinations_with_replacement
可重組合(有放回的組合),即 C(n+r-1, r)
用法
combinations_with_replacement(iterable, r)
示例
from itertools import combinations_with_replacement
res = list(combinations_with_replacement([1, 2, 3, 4], 0))
for i in res:
print(i)
print(u"實際總數:", len(res))
print(u"計算總數:", 10 * 9 * 8 * 7 * 6 * 5 * 4 // (7 * 6 * 5 * 4 * 3 * 2 * 1))
# (1, 1, 1, 1, 1, 1, 1)
# (1, 1, 1, 1, 1, 1, 2)
# (1, 1, 1, 1, 1, 1, 3)
# (1, 1, 1, 1, 1, 1, 4)
# (1, 1, 1, 1, 1, 2, 2)
# (1, 1, 1, 1, 1, 2, 3)
# (1, 1, 1, 1, 1, 2, 4)
# (1, 1, 1, 1, 1, 3, 3)
# (1, 1, 1, 1, 1, 3, 4)
# (1, 1, 1, 1, 1, 4, 4)
# (1, 1,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