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, )]


目录
相关文章
|
5月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
352 1
|
6月前
|
数据采集 存储 XML
Python爬虫技术:从基础到实战的完整教程
最后强调: 父母法律法规限制下进行网络抓取活动; 不得侵犯他人版权隐私利益; 同时也要注意个人安全防止泄露敏感信息.
911 19
|
5月前
|
索引 Python
Python 列表切片赋值教程:掌握 “移花接木” 式列表修改技巧
本文通过生动的“嫁接”比喻,讲解Python列表切片赋值操作。切片可修改原列表内容,实现头部、尾部或中间元素替换,支持不等长赋值,灵活实现列表结构更新。
241 1
|
6月前
|
存储 大数据 Unix
Python生成器 vs 迭代器:从内存到代码的深度解析
在Python中,处理大数据或无限序列时,迭代器与生成器可避免内存溢出。迭代器通过`__iter__`和`__next__`手动实现,控制灵活;生成器用`yield`自动实现,代码简洁、内存高效。生成器适合大文件读取、惰性计算等场景,是性能优化的关键工具。
343 2
|
7月前
|
传感器 数据采集 监控
Python生成器与迭代器:从内存优化到协程调度的深度实践
简介:本文深入解析Python迭代器与生成器的原理及应用,涵盖内存优化技巧、底层协议实现、生成器通信机制及异步编程场景。通过实例讲解如何高效处理大文件、构建数据流水线,并对比不同迭代方式的性能特点,助你编写低内存、高效率的Python代码。
296 0
|
6月前
|
数据采集 存储 JSON
使用Python获取1688商品详情的教程
本教程介绍如何使用Python爬取1688商品详情信息,涵盖环境配置、代码编写、数据处理及合法合规注意事项,助你快速掌握商品数据抓取与保存技巧。
|
6月前
|
大数据 数据处理 数据安全/隐私保护
Python3 迭代器与生成器详解:从入门到实践
简介:本文深入解析Python中处理数据序列的利器——迭代器与生成器。通过通俗语言与实战案例,讲解其核心原理、自定义实现及大数据处理中的高效应用。
308 0
|
7月前
|
并行计算 算法 Java
Python3解释器深度解析与实战教程:从源码到性能优化的全路径探索
Python解释器不止CPython,还包括PyPy、MicroPython、GraalVM等,各具特色,适用于不同场景。本文深入解析Python解释器的工作原理、内存管理机制、GIL限制及其优化策略,并介绍性能调优工具链及未来发展方向,助力开发者提升Python应用性能。
448 0
|
存储 安全 API
【Python 基础教程 21】Python3 文件操作全面指南:从入门到精通的综合教程
【Python 基础教程 21】Python3 文件操作全面指南:从入门到精通的综合教程
488 0
|
数据可视化 IDE 开发工具
【Python篇】PyQt5 超详细教程——由入门到精通(中篇二)
【Python篇】PyQt5 超详细教程——由入门到精通(中篇二)
1112 13

推荐镜像

更多