在本教程中,我們將學習如何使用 Python 獲得給定數據的排列和組合。我們將使用 Python 內置包來查找給定數字的排列和組合。
排列組合是數學中必不可少的一部分。 Python 提供了 itertools 庫,該庫具有計算排列和組合的內置函數。
導入所需的庫
為了計算排列和組合,我們需要導入 itertools 庫。我們可以使用下面的命令導入它。
import itertools
上面的語句將導入 itertools 庫,並形成其功能的路徑。
現在,我們需要創建一個序列列表作為輸入。這個輸入列表將返回由排列和組合組成的元組。我們還可以設置排列組合的長度。
排列
排列是一個集合的排列,其中順序很重要。 Python itertools 模塊提供了內置的置換()方法來查找置換。讓我們理解下面的例子。
示例-
from itertools import permutations
seq = permutations(['1','2','3'])
print(seq)
for p in list(seq):
print(p)
輸出:
('1', '2', '3')
('1', '3', '2')
('2', '1', '3')
('2', '3', '1')
('3', '1', '2')
('3', '2', '1')
在上面的代碼中,我們已經導入了 itertools 模塊。我們稱之為置換()方法,它以字元串作為參數,並提供一個 itertools 對象。有必要使用 for
循環來獲得每個置換。
我們來看兩組排列。
示例- 2
from itertools import permutations
seq = permutations(['A','B'])
for p in list(seq):
print(p)
輸出:
('A', 'B')
('A', 'C')
('B', 'C')
示例- 3
from itertools import permutations
list1 = [1, 2, 3, 4]
seq = permutations(list1)
print(seq)
for p in list(seq):
print(p)
輸出:
(1, 2, 3, 4)
(1, 2, 4, 3)
(1, 3, 2, 4)
(1, 3, 4, 2)
(1, 4, 2, 3)
(1, 4, 3, 2)
(2, 1, 3, 4)
(2, 1, 4, 3)
(2, 3, 1, 4)
(2, 3, 4, 1)
(2, 4, 1, 3)
(2, 4, 3, 1)
(3, 1, 2, 4)
(3, 1, 4, 2)
(3, 2, 1, 4)
(3, 2, 4, 1)
(3, 4, 1, 2)
(3, 4, 2, 1)
(4, 1, 2, 3)
(4, 1, 3, 2)
(4, 2, 1, 3)
(4, 2, 3, 1)
(4, 3, 1, 2)
(4, 3, 2, 1)
在上面的代碼中,我們得到了多個整數的組合。
固定長度的置換
我們可以計算固定長度集合的置換,其中我們只取每個元素置換的指定數量。讓我們理解下面的例子。
示例-
from itertools import permutations
seq = permutations(['H', 'e', 'l', 'l', 'o'], 3)
for p in list(seq):
print(p)
輸出:
('H', 'e')
('H', 'l')
('H', 'l')
('H', 'o')
('e', 'H')
('e', 'l')
('e', 'l')
('e', 'o')
('l', 'H')
('l', 'e')
('l', 'l')
('l', 'o')
('l', 'H')
('l', 'e')
('l', 'l')
('l', 'o')
('o', 'H')
('o', 'e')
('o', 'l')
('o', 'l')
在上面的代碼中,我們通過傳遞長度為 2 來計算固定置換。
字元串的組合
組合是順序無關緊要的元素的集合。Python itertools 模塊提供了組合()方法來計算給定數據的組合。我們可以計算一個字元串的組合。讓我們理解下面的例子。
示例-
import itertools
seq = "ABC"
com_seq = itertools.combinations(seq, 2)
for c in com_seq:
print(c)
輸出:
('A', 'B')
('A', 'C')
('B', 'C')
結合更換
itertools 模塊由另一個名為combination with replacement()的方法組成,該方法也考慮了數字本身的組合。讓我們理解它的例子。
數字集的組合
from itertools import combinations_with_replacement
com = combinations_with_replacement(['J', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't'], 2)
#Print the list of combinations
for c in list(com):
print(c)
輸出:
('J', 'J')
('J', 'a')
('J', 'v')
('J', 'a')
('J', 't')
('J', 'p')
('J', 'o')
('J', 'i')
('J', 'n')
('J', 't')
('a', 'a')
('a', 'v')
('a', 'a')
('a', 't')
('a', 'p')
('a', 'o')
('a', 'i')
('a', 'n')
('a', 't')
('v', 'v')
('v', 'a')
('v', 't')
('v', 'p')
('v', 'o')
('v', 'i')
('v', 'n')
('v', 't')
('a', 'a')
('a', 't')
('a', 'p')
('a', 'o')
('a', 'i')
('a', 'n')
('a', 't')
('t', 't')
('t', 'p')
('t', 'o')
('t', 'i')
('t', 'n')
('t', 't')
('p', 'p')
('p', 'o')
('p', 'i')
('p', 'n')
('p', 't')
('o', 'o')
('o', 'i')
('o', 'n')
('o', 't')
('i', 'i')
('i', 'n')
('i', 't')
('n', 'n')
('n', 't')
('t', 't')
數字集的組合
如果給定的輸入是按排序順序的,組合元組將按排序順序返回。讓我們理解下面的例子。
示例-
import itertools
v = [1, 2, 3, 4]
com_seq = itertools.combinations_with_replacement(v, 3)
for i in com_seq:
print(i)
輸出:
(1, 1, 1)
(1, 1, 2)
(1, 1, 3)
(1, 1, 4)
(1, 2, 2)
(1, 2, 3)
(1, 2, 4)
(1, 3, 3)
(1, 3, 4)
(1, 4, 4)
(2, 2, 2)
(2, 2, 3)
(2, 2, 4)
(2, 3, 3)
(2, 3, 4)
(2, 4, 4)
(3, 3, 3)
(3, 3, 4)
(3, 4, 4)
(4, 4, 4)
在本教程中,我們討論了 itertools 模塊,使用 Python 腳本來查找給定數據的排列和組合。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/288435.html