简体   繁体   English

使用 map-reduce/itertools 对嵌套的迭代器求和

[英]summing nested iterables with map-reduce/itertools

I've been stuck with this data structure for a while now:我已经被这个数据结构困住了一段时间:

iter([iter([1,0]),iter([1,1]),iter([0,0])])

I want to get to sum of the inner-most elements using map-reduce/itertools.我想使用 map-reduce/itertools 来计算最内部元素的总和。

I am able to get to the answer fairly quickly using for loops:我能够使用 for 循环相当快地得到答案:

outer_iter = iter([iter([1,0]),iter([1,1]),iter([0,0])])

for inner_iter in outer_iter:
    for inner_list in inner_iter:
        total = total + inner_list

I am struggling to "translate" the code.我正在努力“翻译”代码。

If the data is nested two levels deep, We can use the chain function to concatenate the iterables together, and then let sum(..) calculate the sum of the iterable.如果数据嵌套两层深,我们可以使用chain函数将迭代器连接在一起,然后让sum(..)计算迭代器的总和。 So:所以:

from itertools import chain

sum(chain.from_iterable(outer_iter))

chain.from_iterable takes as input an iterable of iterables, and converts this into an iterable that lazily obtains the elements from the iterables one at a time. chain.from_iterable将一个可迭代chain.from_iterable作为输入,并将其转换为一个可迭代对象,一次一个地从迭代对象中懒惰地获取元素。 We can use iterable unpacking on chain , but if the outer iterable is an infinite list, the algorithm would get stuck (and eventually run out of memory).我们可以在chain上使用迭代解包,但是如果外部迭代是一个无限列表,算法就会卡住(并最终耗尽内存)。

This is an iterator of iterator of int's.这是 int 的迭代器的迭代器。

  1. Flatten to obtain just an iterator of int's展平以获取 int 的迭代器
  2. Sum this iterator总结这个迭代器

We can use Pyterator for this (disclaimer: I'm the author).我们可以为此使用Pyterator (免责声明:我是作者)。

from pyterator import iterate

iterate(outer_iter).flatten().sum()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM