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.

1
  • Can the iterables be nested arbitrary deep, or only two levels? Commented Dec 1, 2017 at 9:47

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

something I noticed when playing with this code: stackoverflow.com/questions/29403401/…
@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)
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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.