[英]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
上使用迭代解包,但是如果外部迭代是一个无限列表,算法就会卡住(并最终耗尽内存)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.