itertools.product消除重复元素

时间:2013-11-02 17:10:18

标签: python python-2.7 itertools

当我使用itertools.product时,如何跳过迭代中具有重复元素的元组?或者说,无论如何不要在迭代中查看它们?因为如果列表的数量太多,跳过可能会非常耗时。

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在这里重复。我怎么能这样做?

3 个答案:

答案 0 :(得分:10)

itertools通常适用于输入中的唯一位置,而不是唯一的。因此,当您想要删除重复值时,通常必须对itertools结果序列进行后处理,或者“自己动手”。因为在这种情况下后期处理效率非常低,所以请自行编写:

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)]

这可以更有效率,因为从不考虑重复值(既不在输入可迭代内也不在它们之间)。

答案 1 :(得分:5)

lis1 = [1,2]
lis2 = [2,4]
lis3 = [5,6]
from itertools import product
print [i for i in product(lis1,lis2,lis3) if len(set(i)) == 3]

<强>输出

[(1, 2, 5), (1, 2, 6), (1, 4, 5), (1, 4, 6), (2, 4, 5), (2, 4, 6)]

答案 2 :(得分:0)

使用itertools.combinations时,将不会按排序顺序重复元素:

>>> lis = [1, 2, 4, 5, 6]
>>> list(itertools.combinations(lis, 3))
[(1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 4, 5), (1, 4, 6), (1, 5, 6), (2, 4, 5), 
(2, 4, 6), (2, 5, 6), (4, 5, 6)]