假设我有两个列表,列表1和列表2(例如)
l1 = ['w1', 'w2', 'w3', 'w4', 'w5']
l2 = ['w6', 'w7', 'w8']
有没有一个简短而甜蜜的方法来获得一个新的列表(一个给定的固定大小,如4)包含两个列表中的单词,这样每个列表中至少有一个单词((无重复)
大小4的可能结果
['w6', 'w2', 'w4', 'w8']
['w2', 'w8', 'w7', 'w4']
['w1', 'w2', 'w6', 'w4']
['w2', 'w3', 'w1', 'w7']
Tags:
您可以组合这些列表并使用生成器函数:
输出:
您可以生成所有这些:
输出:
l1
的itertools.combinations生成l1
的所有3个长组合,并向其中添加一个l2
元素您可以使用^{} 来实现:
可能的结果:
相关问题 更多 >
编程相关推荐