python 函数产生元组,只需要一个元素

python function yields tuple and only one element is wanted

我有一个功能

def f():
    # whatever
    yield (a,b)

现在我想收集所有 a 但不是 b。另外我希望结果 aa 是一个列表而不是迭代器。现在我用

aa, _ = zip(*f())

就 space/time 效率而言,这是最好的吗?

你不能在不修改 f 的情况下让它只产生元组的一个元素。但是,您可以轻松地链接生成器,例如使用生成器表达式:

just_a_please = (a for a,b in f())

要一次性消耗所有 a,您应该更喜欢列表理解:

all_a = [a for a,b in f()]

如果你只想要其中之一,还有next:

give_me_an_a, _b = next(f())

您可以使用获取第一个返回项的列表理解

aa = [result[0] for result in f()]

很简单,你可以使用列表推导来得到一个包含所有内容的列表

aa = [ a for a,_ in f() ]

zip(*seq) 必须摄取整个生成器,然后才能输出列。这效率不高。

坚持列表理解。您可以使用元组赋值:

aa = [a for a, _ in f()]

或使用索引:

aa = [tup[0] for tup in f()]

如果您没有让所有值都可用于随机访问或其他必须有列表的操作,您可以使用生成器表达式来保持内存效率:

aa = (a for a, _ in f())

对于使用 operator 模块的解决方案:

from operator import itemgetter
get_first = itemgetter(0)
aa = [get_first(x) for x in f()]

编辑:我最初说 "for an efficient solution using the operator module," 但我找不到任何证据表明它比标准列表理解方法更有效。

一些轶事 %timeit 观察:

def f():
    for i in xrange(0, 10000):
        yield (i, i ** i)

def operator_way():
    return [get_first(x) for x in f()]

def tuple_unpack_way():
    return [a for a, _ in f()]

def indexing_way():
    return [a[0] for a in f()]

def map_way():
    return map(get_first, f())

%timeit operator_way() # 100 loops, best of 3: 9.25 ms per loop

%timeit tuple_unpack_way() # 100 loops, best of 3: 9.28 ms per loop

%timeit indexing_way() # 100 loops, best of 3: 9.17 ms per loop

%timeit map_way() # 100 loops, best of 3: 9.07 ms per loop

它们看起来差不多。地图可能会稍微高效一些。