我有以下函数来获取列表的排列次数(不包含重复元素):
import itertools
def permutations_without_repetition(samples, size):
return list(itertools.permutations(samples, size))
只要我提供的列表不包含嵌套列表,它就可以很好地工作。Itertools将嵌套元素视为一个完整的元素,并且不会麻烦地生成包含不同顺序的嵌套列表的排列。
如果我运行permutations_without_repetition([[1, 2], 3], 2)
,我得到的唯一结果是:
[([1, 2], 3), (3, [1, 2])]
我知道这是预期的行为,但我希望结果是:
[([1, 2], 3), (3, [1, 2]), ([2, 1], 3), (3, [2, 1])]
返回包含嵌套列表排列的排列以产生上述结果的最简单方法是什么?
您可以使用递归生成器函数:
def combos(d, c = []):
if not isinstance(d, list):
yield d
elif not d:
yield c
else:
for i, a in enumerate(d):
for k in combos(a, c = []):
yield from combos(d[:i]+d[i+1:], c+[k])
print(list(combos([[1, 2], 3])))
输出:
[[[1, 2], 3], [[2, 1], 3], [3, [1, 2]], [3, [2, 1]]]
使用itertools
的短溶液:
import itertools as it
def combos(d):
if not isinstance(d, list):
yield d
else:
for i in it.permutations(d):
yield from map(list, it.product(*[combos(j) for j in i]))
print(list(combos([[1, 2], 3])))
输出:
[[[1, 2], 3], [[2, 1], 3], [3, [1, 2]], [3, [2, 1]]]