2

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.

I am able to get to the answer fairly quickly using for loops:

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.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
lainglo
  • 51
  • 6

2 Answers2

2

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

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • something I noticed when playing with this code: https://stackoverflow.com/questions/29403401/python-for-loop-and-iterator-behavior – lainglo Dec 05 '17 at 13:21
  • @lainglo: yes, the iterator is indeed consumed. But there is not really a way to not consume the iterator. You can use `tee` etc. that will produce two iterators out of one (by using shared memory) – Willem Van Onsem Dec 05 '17 at 13:47
0

This is an iterator of iterator of int's.

  1. Flatten to obtain just an iterator of int's
  2. Sum this iterator

We can use Pyterator for this (disclaimer: I'm the author).

from pyterator import iterate

iterate(outer_iter).flatten().sum()
remykarem
  • 2,251
  • 22
  • 28