简体   繁体   English

如何在python中将列表中的每个数字相互求和?

[英]How can i sum every number in list with each other in python?

So, i have a list of numbers: 所以,我有一个数字列表:

list = [4,5,65,94,3]

This loop sums the first number in list with every number in list: 此循环将列表中的第一个数字与列表中的每个数字相加:

for i in list:
    sum = i + list[0]

How can i move to the next item in list so that my loop would sum every number in that list with each other? 我如何移动到列表中的下一个项目,以便我的循环将列表中的每个数字相互求和?

So, there's two misconceptions here. 因此,这里有两个误解。

First, that loop isn't doing what you describe. 首先,该循环没有按照您的描述进行。 Provided that sum is defined outside of it, the only values you are adding that will stick after the loop is the last element in the list and whatever is in the first position of the list. 假设sum是在其外部定义的,则添加的唯一值将在循环后保留,是列表中的最后一个元素以及列表的第一个位置中的任何元素。 For your current list, the sum would be 7. 对于您当前的列表,总和为7。

Second, there's a handy function to do this already - sum - which would make short work of your task. 其次,已经有一个方便的功能可以做到这一点- sum -这将使您的任务变得很简单。

sum([4,5,65,94,3])

But if you insist on doing this yourself, you have to set up a few things: 但是,如果您坚持要自己这样做,则必须设置一些内容:

  • Ensure the variable you're accumulating to is a sentinel value for your math operation. 确保要累加的变量是数学运算的前哨值。 For addition, it has to start at 0. For multiplication, it starts at 1. 对于加法,它必须从0开始。对于乘法,它必须从1开始。
  • Add to your original value; 增加到您的原始价值; don't overwrite its value. 不要覆盖其价值。 This would mean either total = total + i or total += i . 这将意味着total = total + itotal += i
  • Remember that Python's for loop is actually a for-each loop. 请记住,Python的for循环实际上是for-each循环。 i will advance through your list and be bound to each value in turn. i将通过您的列表,并依次绑定到每个值。

To fix the code, you'd want to do this: 要修复代码,您需要执行以下操作:

total = 0
li = [4,5,65,94,3]
for i in li:
    total += i

As a final note, avoid shadowing function names. 最后,请避免遮盖函数名称。 list and sum are already defined as functions, and you really don't want to go shadowing them. listsum已经定义为函数,并且您真的不想掩盖它们。

Not exactly sure what you are trying. 不确定您要尝试什么。 To find the sum of a list in python, you just need to do this: 要在python中找到列表的总和,只需执行以下操作:

for i in li:
    total += i

What you are currently doing is changing the value of sum each iteration to equal the i plus the first element of your list. 您当前正在做的是将每次迭代的总和值更改为等于i加列表的第一个元素。 This will make your sum equal to 7 at the end of the loop because it sums the final element (3) with the first element (4). 这将使您的总和在循环结束时等于7,因为它将最后一个元素(3)与第一个元素(4)相加。

Alternatively, python has a built in function to sum the numbers in a list so your code can be simplified to this: 另外,python有一个内置函数可以对列表中的数字求和,因此您的代码可以简化为:

li = [4,5,65,94,3]
total = sum(li)

Edit: As others have pointed out, avoid naming your variables the same as functions. 编辑:正如其他人指出的那样,避免将变量命名为与函数相同。

Or if you do want to iterate using indexes, you can do that using the range function like this 或者,如果您确实想使用索引进行迭代,则可以使用以下范围函数来实现

for i in range(len(list)): 对于范围内的我(len(list)):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM