Python 教程之控制流(12)组合迭代器

简介: Python 教程之控制流(12)组合迭代器

组合迭代器

用于简化组合构造(如排列、组合和笛卡尔积)的递归生成器称为组合迭代器。

在Python中,有4个组合迭代器:

  • 产品(): 此工具计算输入可迭代对象的笛卡尔积。为了计算可迭代对象与自身的乘积,我们使用可选的 repeat 关键字参数来指定重复次数。此函数的输出是按排序顺序排列的元组。
    例:

# 从 itertools 模块导入产品功能
from itertools import product
print("The cartesian product using repeat:")
print(list(product([1, 2], repeat = 2)))
print()
print("The cartesian product of the containers:")
print(list(product(['geeks', 'for', 'geeks'], '2')))
print()
print("The cartesian product of the containers:")
print(list(product('AB', [3, 4])))

输出:

The cartesian product using repeat:
[(1, 1), (1, 2), (2, 1), (2, 2)]
The cartesian product of the containers:
[('geeks', '2'), ('for', '2'), ('geeks', '2')]
The cartesian product of the containers:
[('A', 3), ('A', 4), ('B', 3), ('B', 4)]
  • 排列(): 名称不言自明的排列()用于生成可迭代的所有可能的排列。所有元素都根据其位置而不是其值被视为唯一元素。此函数采用可迭代且group_size,如果未指定group_size的值或等于 None,则group_size的值将成为可迭代的长度。
    例:

# 从itertools模块导入产品功能
from itertools import permutations
print ("All the permutations of the given list is:")
print (list(permutations([1, 'geeks'], 2)))
print()
print ("All the permutations of the given string is:")
print (list(permutations('AB')))
print()
print ("All the permutations of the given container is:")
print(list(permutations(range(3), 2)))

输出:

All the permutations of the given list is:
[(1, 'geeks'), ('geeks', 1)]
All the permutations of the given string is:
[('A', 'B'), ('B', 'A')]
All the permutations of the given container is:
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
  • 组合(): 此迭代器按排序顺序打印在指定组大小中传入的参数的所有可能组合(不带替换)
    例:

# 从itertools模块导入组合
from itertools import combinations
print ("All the combination of list in sorted order(without replacement) is:")
print(list(combinations(['A', 2], 2)))
print()
print ("All the combination of string in sorted order(without replacement) is:")
print(list(combinations('AB', 2)))
print()
print ("All the combination of list in sorted order(without replacement) is:")
print(list(combinations(range(2), 1)))

输出:

All the combination of list in sorted order(without replacement) is:
[('A', 2)]
All the combination of string in sorted order(without replacement) is:
[('A', 'B')]
All the combination of list in sorted order(without replacement) is:
[(0, ), (1, )]
  • Combinations_with_replacement(): 此函数从可迭代元素返回长度为 n 的子序列,其中 n 是函数确定函数生成的子序列长度的参数。单个元素可能会在combinations_with_replacement功能中重复出现。
    例:

# 从itertools模块导入组合
from itertools import combinations_with_replacement
print ("All the combination of string in sorted order(with replacement) is:")
print(list(combinations_with_replacement("AB", 2)))
print()
print ("All the combination of list in sorted order(with replacement) is:")
print(list(combinations_with_replacement([1, 2], 2)))
print()
print ("All the combination of container in sorted order(with replacement) is:")
print(list(combinations_with_replacement(range(2), 1)))

输出:

All the combination of string in sorted order(with replacement) is:
[('A', 'A'), ('A', 'B'), ('B', 'B')]
All the combination of list in sorted order(with replacement) is:
[(1, 1), (1, 2), (2, 2)]
All the combination of container in sorted order(with replacement) is:
[(0, ), (1, )]


目录
相关文章
|
13天前
|
机器学习/深度学习 算法 搜索推荐
从理论到实践,Python算法复杂度分析一站式教程,助你轻松驾驭大数据挑战!
【10月更文挑战第4天】在大数据时代,算法效率至关重要。本文从理论入手,介绍时间复杂度和空间复杂度两个核心概念,并通过冒泡排序和快速排序的Python实现详细分析其复杂度。冒泡排序的时间复杂度为O(n^2),空间复杂度为O(1);快速排序平均时间复杂度为O(n log n),空间复杂度为O(log n)。文章还介绍了算法选择、分而治之及空间换时间等优化策略,帮助你在大数据挑战中游刃有余。
41 4
|
10天前
|
数据采集 Web App开发 数据可视化
Python爬虫教程:Selenium可视化爬虫的快速入门
Python爬虫教程:Selenium可视化爬虫的快速入门
|
19天前
|
监控 数据可视化 搜索推荐
【Python篇】matplotlib超详细教程-由入门到精通(下篇)2
【Python篇】matplotlib超详细教程-由入门到精通(下篇)
29 8
|
16天前
|
缓存 测试技术 Apache
告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
【10月更文挑战第1天】告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
45 4
|
19天前
|
存储 索引 Python
Python 迭代器是怎么实现的?
Python 迭代器是怎么实现的?
22 6
|
19天前
|
编解码 数据可视化 IDE
【Python篇】matplotlib超详细教程-由入门到精通(下篇)1
【Python篇】matplotlib超详细教程-由入门到精通(下篇)
26 3
|
18天前
|
存储 大数据 Python
Python 中迭代器与生成器:深度解析与实用指南
Python 中迭代器与生成器:深度解析与实用指南
12 0
|
28天前
|
机器学习/深度学习 设计模式 大数据
30天拿下Python之迭代器和生成器
30天拿下Python之迭代器和生成器
17 3
|
5月前
|
Python
【Python操作基础】——字典,迭代器和生成器
【Python操作基础】——字典,迭代器和生成器
|
3月前
|
Python
python解包迭代器或生成器
【7月更文挑战第1天】
30 3