列表对象的全部组合
打印给定列表中所有对象的组合是我们在给定列表上可以执行的常见操作之一。Python 的 'itertools' 模块提供了一些内置方法,这些方法高效且易于使用,从而简化了生成列表对象可能组合的过程。我们将通过本文学习如何使用 'itertools' 模块。
Python 程序打印列表对象的全部组合
让我们讨论 itertools 的内置方法以及它们的示例程序,这些程序将向我们展示如何为列表对象生成组合。
itertools
这是一个快速且内存高效的工具,用于处理可迭代对象。要在我们的程序中使用此模块,我们需要使用以下命令导入它
import itertools
combinations()
当我们处理排列和组合时,此方法最适合。它接受两个参数,并从对象列表中生成给定长度的所有可能的组合。
语法
combinations(nameOfiterator, length)
这里
nameOfiterator 指定我们需要组合的可迭代对象。
length 指定组合的长度。
示例 1
以下示例说明了如何使用 'combinations()' 方法为列表对象生成组合。
方法
第一步是导入 'itertools' 模块。
创建一个名为 'get_combinations()' 的用户定义方法以及一个参数。
初始化一个名为 'combination' 的空列表以存储所有组合。
使用 for 循环迭代输入列表,范围从 1 到列表的长度。
现在,调用 'itertools.combinations()' 方法从指定的输入列表生成长度为 r 的所有可能的组合。此外,我们需要通过附加生成的组合来扩展组合列表。
创建另一个名为 'objects' 的列表,其中包含三个元素。
最后,使用 'objects' 作为参数调用 'get_combinations()' 方法以生成组合。
import itertools
def get_combinations(lst): # creating a user-defined method
combination = [] # empty list
for r in range(1, len(lst) + 1):
# to generate combination
combination.extend(itertools.combinations(lst, r))
return combination
objects = ['9', '8', '0'] # creating a list named objects
all_combinations = get_combinations(objects) # method call
print(all_combinations)
输出
[('9',), ('8',), ('0',), ('9', '8'), ('9', '0'), ('8', '0'), ('9', '8', '0')]
示例 2
在此示例中,我们将使用 '+=' 运算符而不是 'extend' 关键字将所有组合附加到一个列表中。
import itertools
def get_combinations(lst): # creating a user-defined method
combination = [] # empty list
for r in range(1, len(lst) + 1):
# to generate combination
combination += itertools.combinations(lst, r)
return combination
objects = ['9', '8', '0'] # creating a list named objects
all_combinations = get_combinations(objects) # method call
print(all_combinations)
输出
[('9',), ('8',), ('0',), ('9', '8'), ('9', '0'), ('8', '0'), ('9', '8', '0')]
product()
此方法用于返回指定迭代器的笛卡尔积。它接受一个可迭代对象和一个整数作为参数。此处的整数指定对象的重复次数。
语法
combinations(nameOfiterator, repeat = r)
示例 3
在此示例中,我们将使用上一示例中的代码并进行一些更改。我们将使用内置方法 'product()' 而不是 'combinations()'。其余代码的工作方式与前一个相同,但它允许重复对象。
import itertools
def get_combinations(lst): # creating a user-defined function
combination = [] # empty list
for r in range(1, len(lst) + 1):
# to generate combination
combination.extend(itertools.product(lst, repeat=r))
return combination
objects = ['9', '8', '0'] # creating a list named objects
all_combinations = get_combinations(objects)
print(all_combinations)
输出
[('9',), ('8',), ('0',), ('9', '9'), ('9', '8'), ('9', '0'), ('8', '9'), ('8',
'8'), ('8', '0'), ('0', '9'), ('0', '8'), ('0', '0'), ('9', '9', '9'), ('9', '9', '8'), ('9', '9', '0'), ('9',
'8', '9'), ('9', '8', '8'), ('9', '8', '0'), ('9', '0', '9'), ('9', '0', '8'), ('9', '0', '0'), ('8', '9', '9'),
('8', '9', '8'), ('8', '9', '0'), ('8', '8', '9'), ('8', '8', '8'), ('8', '8', '0'), ('8', '0', '9'), ('8', '0',
'8'), ('8', '0', '0'), ('0', '9', '9'), ('0', '9', '8'), ('0', '9', '0'), ('0', '8', '9'), ('0', '8', '8'),
('0', '8', '0'), ('0', '0', '9'), ('0', '0', '8'), ('0', '0', '0')]
结论
我们从解决给定问题并介绍可能的解决方案开始本文。然后,在后面的部分中,我们学习了 'itertools' 模块及其内置方法。在示例程序的帮助下,我们讨论了这些方法在为列表对象生成所有组合中的用法。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP