问题描述
当我使用 itertools.product
?还是说,是否在迭代中不去查看它们?因为如果列表数量太多,跳过可能会很耗时.
How can I skip the tuples which has duplicate elements in the iteration when I use itertools.product
? Or let's say, is there anyway not to look at them in the iteration? Because skipping may be time consuming if the number of lists are too much.
Example,
lis1 = [1,2]
lis2 = [2,4]
lis3 = [5,6]
[i for i in product(lis1,lis2,lis3)] should be [(1,2,5), (1,2,6), (1,4,5), (1,4,6), (2,4,5), (2,4,6)]
它将没有(2,2,5)
和(2,2,6)
,因为此处2是重复的.我该怎么办?
It will not have (2,2,5)
and (2,2,6)
since 2 is duplicate in here. How can I do that?
推荐答案
itertools
通常适用于输入中唯一的位置,而不适用于唯一的值.因此,当您要删除重复的值时,通常必须对itertools
结果序列进行后处理,或者自行滚动".因为在这种情况下后期处理效率可能非常低,所以请自己动手:
itertools
generally works on unique positions within inputs, not on unique values. So when you want to remove duplicate values, you generally have to either post-process the itertools
result sequence, or "roll your own". Because post-processing can be very inefficient in this case, roll your own:
def uprod(*seqs):
def inner(i):
if i == n:
yield tuple(result)
return
for elt in sets[i] - seen:
seen.add(elt)
result[i] = elt
for t in inner(i+1):
yield t
seen.remove(elt)
sets = [set(seq) for seq in seqs]
n = len(sets)
seen = set()
result = [None] * n
for t in inner(0):
yield t
然后,例如
>>> print list(uprod([1, 2, 1], [2, 4, 4], [5, 6, 5]))
[(1, 2, 5), (1, 2, 6), (1, 4, 5), (1, 4, 6), (2, 4, 5), (2, 4, 6)]
>>> print list(uprod([1], [1, 2], [1, 2, 4], [1, 5, 6]))
[(1, 2, 4, 5), (1, 2, 4, 6)]
>>> print list(uprod([1], [1, 2, 4], [1, 5, 6], [1]))
[]
>>> print list(uprod([1, 2], [3, 4]))
[(1, 3), (1, 4), (2, 3), (2, 4)]
这可能会更加高效,因为甚至都不会考虑重复的值(既不可在输入内迭代,也不能跨输入).
This can be much more efficient, since a duplicate value is never even considered (neither within an input iterable, nor across them).
这篇关于itertools.product消除重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!