Python 获取数字列表并返回平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16257598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Take a list of numbers and return the average
提问by Lesley Hawkes
Doing GCSE computing and as a homework task I need to do the below. I'm only starting out with programming and I've been trying to figure out how to do it but to no avail. I believe I need to use a functionbut searching "python function list" etc gives me no help when I try it.
做 GCSE 计算并作为家庭作业,我需要做以下事情。我只是从编程开始,我一直试图弄清楚如何去做,但无济于事。我相信我需要使用function但搜索“python 函数列表”等在我尝试时没有帮助。
Can you just tell me how to:
你能告诉我如何:
Ask user to input a "list of numbers"
Print these numbers out for confirmation
Convert them to a variable(s)?
Add them together
Divide sum by number of numbers entered - Not even the slightest clue as to how do that!
Finally, print Average isand the result.
要求用户输入“数字列表”
打印这些数字以供确认
将它们转换为变量?
将它们加在一起将
总和除以输入的数字数量 - 甚至没有任何关于如何做到这一点的线索!
最后,打印Average is和结果。
What I've got at current:
我目前所拥有的:
print("Welcome, this program will find the average of a list of numbers you enter.")
numbers = input("Enter your numbers, seperated by spaces.")
print("You have entered")
print(numbers)
print(numbers[0])
print(numbers[1])
print(numbers[2])
print(numbers[3])
print(numbers[4])
print(numbers[5])
print(numbers[6])
print(len(numbers))
print("The average of the above numbers is: ") #FURTHEST I'VE GOT
回答by Spencer Moran
You want to iterate through the list, sum all the numbers, and then divide the sum by the number of elements in the list. You can use a for loop to accomplish this.
您想遍历列表,对所有数字求和,然后将总和除以列表中的元素数。您可以使用 for 循环来完成此操作。
average = 0
sum = 0
for n in numbers:
sum = sum + n
average = sum / len(numbers)
The for loop looks at each element in the list, and then adds it to the current sum. You then divide by the length of the list (or the number of elements in the list) to find the average.
for 循环查看列表中的每个元素,然后将其添加到当前总和中。然后除以列表的长度(或列表中的元素数)以求平均值。
I would recommend googling a python reference to find out how to use common programming concepts like loops and conditionals so that you feel comfortable when starting out. There are lots of great resources online that you could look up.
我建议在谷歌上搜索 Python 参考资料,以了解如何使用循环和条件等常见编程概念,以便您在开始时感到舒适。网上有很多很棒的资源,你可以查一下。
Good luck!
祝你好运!
回答by rzzzwilson
The input()function returns a string which may contain a "list of numbers". You should have understood that the numbers[2]operation returns the third element of an iterable. A string isan iterable, but an iterable of characters, which isn't what you want - you want to average the numbersin the input string.
的输入()函数返回,其可含有一个“号码清单”的字符串。您应该已经了解numbers[2]操作返回可迭代对象的第三个元素。字符串是可迭代的,但是是可迭代的字符,这不是您想要的 - 您想要平均输入字符串中的数字。
So there are two things you have to do before you can get to the averaging shown by garyprice:
因此,在获得 garyprice 显示的平均值之前,您必须做两件事:
- convert the input string into something containing just the number strings (you don't want the spaces between the numbers)
- convert each number string into an integer
- 将输入字符串转换为仅包含数字字符串的内容(您不希望数字之间有空格)
- 将每个数字字符串转换为整数
Hint for step 1: you have to split the input string into non-space substrings.
第 1 步提示:您必须将输入字符串拆分为非空格子字符串。
Step 2 (convert string to integer) should be easy to find with google.
第 2 步(将字符串转换为整数)应该很容易用 google 找到。
HTH
HTH
回答by Mohammed Yasin
You can use python's built-in function sum
你可以使用python的内置函数 sum
sumwill return the sum of all the valueslento get list's length
sum将返回所有值的总和len获取列表的长度
code:
代码:
>>> list = [1,2,3,4]
>>> sum(list)
>>> 10
>>> len(list)
>>> 4
>>> avg = float(sum(list))/len(list)
>>> 2.5
>>>"""In pyton3 don't want to specify float"""
>>> 10 / 4
>>> 2.5
Use float because when using python 2.x, because:
使用 float 是因为在使用 python 2.x 时,因为:
int/intreturns int value (i.e. 2)float/intreturns float value (i.e. 2.5)
int/int返回 int 值(即 2)float/int返回浮点值(即 2.5)
While in Python 3.x:
在 Python 3.x 中:
int/intreturn floatint//intreturn int
int/int返回浮动int//int返回整数
回答by seeiespi
If you have the numpypackage:
如果您有numpy包裹:
In [16]: x = [1,2,3,4]
...: import numpy
...: numpy.average(x)
Out[16]: 2.5
回答by Israel Manzo
Simple math..
简单的数学..
def average(n):
result = 0
for i in n:
result += i
ave_num = result / len(n)
return ave_num
input -> [1,2,3,4,5]
output -> 3.0

