如何在python中对数字列表求和 [英] How to sum a list of numbers in python
问题描述
所以首先,我需要从 455,111,451 到 455,112,000 范围内提取数字.我可以手动执行此操作,我只需要 50 个号码,但这不是重点.
我试图:
for a in range(49999951,50000000):打印 +str(a)
我该怎么办?
使用 <代码>和
<预><代码>>>>总和(范围(49999951,50000000))2449998775L<小时>
这是一个内置函数,这意味着你不需要导入任何东西或做任何特殊的事情来使用它.在您来这里询问之前,您应该始终查阅文档或教程,以防它已经存在 - 此外,StackOverflow 有一个搜索功能也可以帮助您找到问题的答案.
<小时>sum
函数在这个case,取一个整数列表,并以类似的方式将它们彼此递增地相加,如下所示:
也 - 类似于 Reduce
:
So first of all, I need to extract the numbers from a range of 455,111,451, to 455,112,000. I could do this manually, there's only 50 numbers I need, but that's not the point.
I tried to:
for a in range(49999951,50000000):
print +str(a)
What should I do?
Use sum
>>> sum(range(49999951,50000000))
2449998775L
It is a builtin function, Which means you don't need to import anything or do anything special to use it. you should always consult the documentation, or the tutorials before you come asking here, in case it already exists - also, StackOverflow has a search function that could have helped you find an answer to your problem as well.
The sum
function in this case, takes a list of integers, and incrementally adds them to eachother, in a similar fashion as below:
>>> total = 0
>>> for i in range(49999951,50000000):
total += i
>>> total
2449998775L
Also - similar to Reduce
:
>>> reduce(lambda x,y: x+y, range(49999951,50000000))
2449998775L
这篇关于如何在python中对数字列表求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!