列表物件的全部組合


列印給定列表中所有物件的組合是我們在給定列表上可以執行的常見操作之一。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' 模組及其內建方法。在示例程式的幫助下,我們討論了這些方法在為列表物件生成所有組合中的用法。

更新於: 2023-07-21

12K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告