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, )]
目录
相关文章
|
12天前
|
数据采集 存储 搜索推荐
打造个性化网页爬虫:从零开始的Python教程
【8月更文挑战第31天】在数字信息的海洋中,网页爬虫是一艘能够自动搜集网络数据的神奇船只。本文将引导你启航,用Python语言建造属于你自己的网页爬虫。我们将一起探索如何从无到有,一步步构建一个能够抓取、解析并存储网页数据的基础爬虫。文章不仅分享代码,更带你理解背后的逻辑,让你能在遇到问题时自行找到解决方案。无论你是编程新手还是有一定基础的开发者,这篇文章都会为你打开一扇通往数据世界的新窗。
|
6天前
|
缓存 测试技术 Apache
告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
【9月更文挑战第5天】性能测试是确保应用在高负载下稳定运行的关键。本文介绍Apache JMeter和Locust两款常用性能测试工具,帮助识别并解决性能瓶颈。JMeter适用于测试静态和动态资源,而Locust则通过Python脚本模拟HTTP请求。文章详细讲解了安装、配置及使用方法,并提供了实战案例,帮助你掌握性能测试技巧,提升应用性能。通过分析测试结果、模拟并发、检查资源使用情况及代码优化,确保应用在高并发环境下表现优异。
28 5
|
14天前
|
数据采集 存储 大数据
Python关于迭代器的使用
在Python编程中,数据的处理和操作是核心任务之一。 想象一下,你有一个装满各种颜色球的箱子,你想逐个查看并使用这些球,但又不想一次性将它们全部取出。 这就引出了我们今天要讨论的主题——迭代。
|
15天前
|
前端开发 JavaScript 数据库
python Django教程 之模板渲染、循环、条件判断、常用的标签、过滤器
python Django教程 之模板渲染、循环、条件判断、常用的标签、过滤器
|
14天前
|
Unix Python
python 的标准库模块glob使用教程,主要为glob.glob()使用与glob.iglob()使用
python 的标准库模块glob使用教程,主要为glob.glob()使用与glob.iglob()使用
8 0
|
15天前
|
SQL Shell API
python Django教程 之 模型(数据库)、自定义Field、数据表更改、QuerySet API
python Django教程 之 模型(数据库)、自定义Field、数据表更改、QuerySet API
|
Python
Python 控制流和函数
控制流 条件语句 Python只有一种条件语句,那就是if语句。如果需要多重分支,使用if-elif-else结构。Python没有switch语句,条件语句只有if这么一种。
1072 0
|
1天前
|
存储 数据采集 人工智能
探索Python编程之美——从基础到进阶
【9月更文挑战第9天】本文是一篇深入浅出的技术分享文章,旨在引导读者从零基础开始掌握Python编程。我们将通过生动的实例和代码示例,探讨Python的基本语法、数据结构、函数、模块以及面向对象编程等核心概念。无论你是初学者还是有一定经验的开发者,都能在这篇文章中找到有价值的内容。让我们一起开启Python编程之旅吧!
16 11
|
2天前
|
Python
探索Python编程的奥秘:打造你的第一个程序
【9月更文挑战第8天】本文将带你进入Python编程的世界,通过一个有趣的项目——制作一个简单的猜数字游戏,让你快速入门。我们不仅会分享代码编写的步骤,还会讲解每一行代码的含义和作用,确保即使是编程新手也能跟上节奏。文章末尾附有完整代码,方便读者实践和学习。
18 12
|
3天前
|
API Python
探索Python中的多线程编程
探索Python中的多线程编程
20 5