Python – 從其他列表獲取所有替換組合
當需要從其他列表獲取替換組合時,可以使用 “combinations” 方法和 “list” 方法。
示例
以下是一個演示
from itertools import combinations
my_list = [54, 98, 11]
print("The list is :")
print(my_list)
replace_list = [8, 10]
my_result = list(combinations(my_list + replace_list, len(my_list)))
print("The result is :")
print(my_result)輸出
The list is : [54, 98, 11] The result is : [(54, 98, 11), (54, 98, 8), (54, 98, 10), (54, 11, 8), (54, 11, 10), (54, 8, 10), (98, 11, 8), (98, 11, 10), (98, 8, 10), (11, 8, 10)]
說明
將所需的軟體包匯入到環境中。
定義一個列表並顯示在控制檯中。
定義另一個替換列表。
使用 “combinations” 方法將原始列表、替換列表和原始列表的長度串聯起來。
將其轉換為列表。
將其分配給一個變數。
結果顯示在控制檯中。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP