应用地图功能时访问生成器项目

时间:2019-06-27 14:23:28

标签: python python-3.x generator

说我有一个功能:

def plus(x):
    return [x+1, x+2]

以及一些我想将上述功能应用到的数据:

 data=(i for i in range(5)) #large, don't fit in ram

我正在遍历map函数的结果。当map函数的结果满足特定类别时,我正在尝试获取数据变量的值:

for result in map(plus, data) or []:
     if result >3:
        print(f"{result} -xx")

如何访问符合此条件的生成器项(上面标记为xx)?我能想到的一种方法是将plus函数中的变量x附加到要返回的列表中,但这似乎是多余的。

1 个答案:

答案 0 :(得分:1)

The itertools.tee function使类似的事情变得实用(并且将它用于并行迭代是它实际上节省内存的唯一情况,因此,这是完美的情况):

if (tempCount == count && temp > repeated)
{
   // updates for EQUAL count, but only for larger numbers
   repeated = temp;
   count = tempCount;
}
if (tempCount > count)
{
  // updates for larger count, small/large value doesn't matter
  repeated = temp;
  count = tempCount;
}

请注意,我删除了import itertools # Make two iterators that will each produce the original data once # When the value has been produced by one iterator, it is cached internally; # when the second iterator produces it, it's discarded data, data2 = itertools.tee(data) # Iterate original data and mapped data in parallel for x, result in zip(data, map(plus, data2)): if result >3: print(f"{result} {x}") ,因为它保证毫无意义; or []对象始终是真实的,因此您总是要迭代map对象(即使结果为空,它也可以正常工作)。