我正在寻找在Python中解决此问题的所有可能组合:
list1 = ['M','W','D']
list2 = ['y','n']
What i'm looking to have is:
[[('M', 'y'), ('W', 'n'), ('D', 'n')], [('M', 'y'), ('D', 'n'), ('W', 'n'), ....
我需要像这样拥有M、W、D的所有可能性:
M W D
y n y
y y y
y y n
y n n
n y y
n n y
. . .
我尝试了:
import itertools
list1 = ['M','W','D']
list2 = ['y','n']
all_combinations = []
list1_permutations = itertools.permutations(list1, len(list2))
for each_permutation in list1_permutations:
zipped = zip(each_permutation, list2)
all_combinations.append(list(zipped))
print(all_combinations)
我得到:
[[('M', 'y'), ('W', 'n')], [('M', 'y'), ('D', 'n')], [('W', 'y'), ('M', 'n')], [('W', 'y'), ('D', 'n')], [('D', 'y'), ('M', 'n')], [('D', 'y'), ('W', 'n')]]
这绝对不是我所需要的,因为我需要将列表1的所有元素显示在同一个列表中,并与列表2的元素组合。
我不确定列表是否适用于此问题,但我需要' D'、' W'、' M'和' y'、' n'的所有可能性。