【问题标题】:Python: compose list from nested iteratorsPython:从嵌套迭代器组成列表
【发布时间】:2013-05-11 20:26:17
【问题描述】:

我有一个元组列表,我需要通过添加两个元素来扩展它,每个元素都来自一个列表。所以我有[(1, 2, 3)] 和两个迭代器it1 = (i for i in ['a1', 'a2'])it2 = (i for i in in ['b1', 'b2'])。结果应该是[(1, 2, 3, 'a1', 'b1'), (1, 2, 3, 'a1', 'b2'), (1, 2, 3, 'a2', 'b1'), (1, 2, 3, 'a2', 'b2')]

如果我使用上面显示的迭代器,它就不起作用。但是,如果我使用列表,它会起作用。代码如下:

def get_iters():
    return ((i for i in ['a1', 'a2']), (i for i in ['b1', 'b2']))

def get_lists():
    return ([i for i in ['a1', 'a2']], [i for i in ['b1', 'b2']])

def compose(lst_of_tuples, iter=True):
    iters = get_iters() if iter else get_lists()
    for it in iters:
        lst_of_tuples = [t + (i,) for t in lst_of_tuples for i in it]
    return lst_of_tuples

print compose([(1,2,3)], True)
# WRONG!???? (what happened to the 'a2' part of it?)
# prints: [(1, 2, 3, 'a1', 'b1'), (1, 2, 3, 'a1', 'b2')]

print compose([(1,2,3)], False)
# RIGHT!! prints: [(1, 2, 3, 'a1', 'b1'), (1, 2, 3, 'a1', 'b2'), (1, 2, 3, 'a2', 'b1'), (1, 2, 3, 'a2', 'b2')]

我想不出为什么会这样。谁能解释一下?

【问题讨论】:

    标签: python functional-programming iterator scope function-composition


    【解决方案1】:

    Iterables 只能迭代一次,之后它们就会被耗尽。

    在 for 循环中第二次遍历给定的可迭代对象时,不再返回任何元素。

    首先循环遍历itertools.product(),然后遍历您的元组列表以生成输出:

    from itertools import product
    
    def compose(lst_of_tuples, iter=True):
        iters = get_iters() if iter else get_lists()
        return [t + i for i in product(*get_iters()) for t in lst_of_tuples]
    

    这会产生:

    >>> print compose([(1,2,3)], True)
    [(1, 2, 3, 'a1', 'b1'), (1, 2, 3, 'a1', 'b2'), (1, 2, 3, 'a2', 'b1'), (1, 2, 3, 'a2', 'b2')]
    >>> print compose([(1,2,3)], False)
    [(1, 2, 3, 'a1', 'b1'), (1, 2, 3, 'a1', 'b2'), (1, 2, 3, 'a2', 'b1'), (1, 2, 3, 'a2', 'b2')]
    

    【讨论】:

    • 好的。我现在明白了。迭代器(it1 或 it2)在 lst_of_tuples 的第一个元素被迭代后用完。所以代码只对第一个元素有效。如果我这样做[t + (i,) for i in it for t in lst_of_tuples,它就会起作用。感谢 itertool.product 的想法 - 整洁。
    猜你喜欢
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2013-02-04
    相关资源
    最近更新 更多