[英]Accumulate sum of all element in list having integers
I'm trying to get the sum of all numbers for each list through itertool accumulate function: This is the code:我试图通过 itertool accumulate function 获取每个列表的所有数字的总和:这是代码:
from itertools import *
import operator
list_numbers = [[45,43,56,67],[41,423,526,627,897],[545,453,656,667]]
Iterable_list = iter(list_numbers)
while(True):
try:
print(list(accumulate(next(Iterable_list),operator.add))[-1])
except StopIteration as ex:
print(ex)
break
The above works well.以上效果很好。
if I try to take out the max of each list:如果我尝试取出每个列表的最大值:
while(True):
try:
print(list(accumulate(next(Iterable_list),func=max))[-1])
except StopIteration as ex:
print(ex)
break
this also works well.这也很好用。
My interpretation was, if I use func = sum
, it will do the sum of each list like I tried below but it prompts error message:我的解释是,如果我使用
func = sum
,它会像我在下面尝试的那样对每个列表求和,但它会提示错误消息:
'int' object is not iterable
while(True):
try:
print(list(accumulate(next(Iterable_list),func=sum))[-1])
except StopIteration as ex:
print(ex)
break
Why it doesn't it work and in which scenario can we use func = sum
?为什么它不起作用,在什么情况下我们可以使用
func = sum
?
You could understand better the behavior of itertools.accumulate
if you use a lambda function to explore the inputs that func
will receive.如果您使用 lambda function 来探索
func
将接收的输入,您可以更好地理解itertools.accumulate
的行为。
Let's use lambda x: print(x)
instead of sum
first, to print the values:让我们先使用
lambda x: print(x)
而不是sum
来打印值:
TypeError: <lambda>() takes 1 positional argument but 2 were given
Ok, so func
will receive two values.好的,所以
func
将收到两个值。 Let's try lambda x, y: print(x, y)
:让我们试试
lambda x, y: print(x, y)
:
45 43
None 56
None 67
None
41 423
None 526
None 627
None 897
None
545 453
None 656
None 667
None
Why are we seeing
None
?为什么我们看到
None
? Becauseprint()
does not return any value, so it returns aNone
.因为
print()
不返回任何值,所以它返回一个None
。
This means that func
should receive two values from the iterator (L1 and L2), apply an operation on both values and return a single value C. Then, apply the same operation on the previous result (C) with the next value in the iterator (L3).这意味着
func
应该从迭代器(L1 和 L2)接收两个值,对这两个值应用操作并返回单个值 C。然后,对迭代器中的下一个值对前一个结果 (C) 应用相同的操作(L3)。
Well, because it expects a single argument, an iterable, and optionally accepts a second ( start
).好吧,因为它需要一个参数,一个可迭代的,并且可以选择接受第二个(
start
)。 Now, itertools.accumulate
is passing two values and Python assumes you're trying to use sum(L1, start=L2)
, but L1
is not iterable since its just a number... TypeError: 'int' object is not iterable
.现在,
itertools.accumulate
正在传递两个值,并且 Python 假设您正在尝试使用sum(L1, start=L2)
,但是L1
不可迭代,因为它只是一个数字... TypeError: 'int' object is not iterable
。
In which scenario can we use func=sum
?在什么情况下我们可以使用
func=sum
?
sum()
could help us if it could receive two values and return a single one, which is possible ( sum(iterable, start=start)
).如果
sum()
可以接收两个值并返回一个值,那么它可以帮助我们,这是可能的( sum(iterable, start=start)
)。 Now, consider that the output of sum
will be used as the first input of sum
with the next element in the iterable, something like: sum(sum(L1, start=L2), start=L3)
and so on.现在,考虑
sum
sum
第一个输入,以及可迭代对象中的下一个元素,例如: sum(sum(L1, start=L2), start=L3)
等等。 This means that sum(L1, start=L2)
should return an iterable for us to be able to use sum
.这意味着
sum(L1, start=L2)
应该返回一个可迭代对象以便我们能够使用sum
。 It's hard to find an example for this, meaning that sum
might not be the best option for any particular case when using itertools.accumulate
, but this doesn't mean that sum
is not useful by itself.很难为此找到示例,这意味着在使用
itertools.accumulate
时sum
可能不是任何特定情况下的最佳选择,但这并不意味着sum
本身没有用。
This can be done using Zip() or map() method or using numpy.这可以使用 Zip() 或 map() 方法或使用 numpy 来完成。
Using zip():使用压缩():
def nested_sum(lst):
return [sum(i) for i in zip(*lst)]
lst = [[2, 4, 6], [2, 2, 1], [1, 2, 3]] # A Sample List
print(nested_sum(lst))
#Output
[12,5,6]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.