如何以這樣的方式組合 2 個串列,即串列 2 中的每個元素都與串列 1 中的元素組合,以便“a”在每次迭代中僅與串列 1 中的一個元素組合,然后“b”僅與串列中的兩個元素組合在每次迭代中列出 1。
list1= [1,2,3]
list2= [‘a’,‘b’,‘c’,‘d’,‘e’]
dict_list_2 = {‘a’ : 1 , ‘b’ : 2 , ‘c’ : 3 , ‘d’ : 4 , ‘e’ :5}
Expected Output: ('a',1) (a ,2 ) (a , 3) (b , 1 ,2) (b , 1 ,3) (b , 2 ,3) (b , 1 ,1) (b , 2 ,2) (b , 3 ,3) –
在此處輸入圖片說明
uj5u.com熱心網友回復:
假設您想要所有組合替換 from list1:
import itertools
list1= [1,2,3]
list2= list("abcde")
dict_list_2 = {"a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 , "e" :5}
[(letter,) product
for letter in list2
for product in itertools.product(list1, repeat=dict_list_2[letter])]
# out:
[('a', 1),
('a', 2),
('a', 3),
('b', 1, 1),
('b', 1, 2),
('b', 1, 3),
('b', 2, 1),
('b', 2, 2),
('b', 2, 3),
('b', 3, 1),
('b', 3, 2),
('b', 3, 3),
('c', 1, 1, 1),
('c', 1, 1, 2),
('c', 1, 1, 3),
('c', 1, 2, 1),
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/352452.html
