Python 为对象列表打印所有组合

Python 为对象列表打印所有组合

打印对象列表的所有组合是我们可以在给定列表上执行的常见操作之一。Python的’itertools’模块提供了一些内置的方法,既高效又易于使用,可以简单地生成可能的对象列表组合。通过本文,我们将学习如何使用’itertools’模块。

Python程序打印对象列表的所有组合

让我们讨论itertools的内置方法以及它们的示例程序,展示了如何生成对象列表的组合。

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’模块及其内置方法。通过示例程序的帮助,我们讨论了这些方法在生成对象列表的所有组合中的使用。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程