【Python、标准库】itertools

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, 1, 1, 2, 2,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3)
# (2, 2, 3, 3, 3, 3, 4)
# (2, 2, 3, 3, 3, 4, 4)
# (2, 2, 3, 3, 4, 4, 4)
# (2, 2, 3, 4, 4, 4, 4)
# (2, 2, 4, 4, 4, 4, 4)
# (2, 3, 3, 3, 3, 3, 3)
# (2, 3, 3, 3, 3, 3, 4)
# (2, 3, 3, 3, 3, 4, 4)
# (2, 3, 3, 3, 4, 4, 4)
# (2, 3, 3, 4, 4, 4, 4)
# (2, 3, 4, 4, 4, 4, 4)
# (2, 4, 4, 4, 4, 4, 4)
# (3, 3, 3, 3, 3, 3, 3)
# (3, 3, 3, 3, 3, 3, 4)
# (3, 3, 3, 3, 3, 4, 4)
# (3, 3, 3, 3, 4, 4, 4)
# (3, 3, 3, 4, 4, 4, 4)
# (3, 3, 4, 4, 4, 4, 4)
# (3, 4, 4, 4, 4, 4, 4)
# (4, 4, 4, 4, 4, 4, 4)
# 实际总数: 120
# 计算总数: 120
发布了52 篇原创文章 · 获赞 143 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/lnotime/article/details/102501807