小编las*_*nce的帖子

python中所有可能的组合

我正在寻找 python 中针对此问题的所有可能组合:

list1 = ['M','W','D']
list2 = ['y','n']
Run Code Online (Sandbox Code Playgroud)

我想要的是:

[[('M', 'y'), ('W', 'n'), ('D', 'n')], [('M', 'y'), ('D', 'n'), ('W', 'n'), ....
Run Code Online (Sandbox Code Playgroud)

我需要像这样拥有 M、W、D 的所有可能性:

M W D

y n y

y y y

y y n

y n n

n y y

n n y

. . .
Run Code Online (Sandbox Code Playgroud)

我试过:

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)
Run Code Online (Sandbox Code Playgroud)

我得到:

[[('M', 'y'), ('W', …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

4
推荐指数
1
解决办法
75
查看次数

标签 统计

python ×1

python-3.x ×1