如何找到数组的平均值并将多个值存储在数组中
how to find the average for a array and to store multiple values in a array
如何将我的程序的多个结果存储在一个数组中?
对于数组中的每个人,我需要存储 3 个结果,但每次我这样做时,当程序为 运行.
时,我都会收到错误消息
程序为运行时,我找不到3个数组中每个人的平均分。
我是 PYTHON 的新手。
import operator
print("if form1 enter x, if form2 enter y,if form 3 enter z")
print("to view all results enter allscore")
x={"guv":1,"jane":4,"adam":5,"luiz":8,"elizabeth":9,"paul":10,"guv":3}
sorted(x.items(), key=operator.itemgetter(1), reverse=True) #sorts x from high to low
sorted(x.items()) #sorts x alphabetically
y={"adrian":0,"anna":9,"david":7,"matt":10,"micheal":5}
sorted(y.items(), key=operator.itemgetter(1), reverse=True)
sorted(y.items())
z={"gus":6,"mohammed":6,"winston":9,"barry":9,"ping":3}
sorted(z.items(), key=operator.itemgetter(1), reverse=True)
sorted(z.items()) #
allscore={}
allscore.update(x) # to get data from array x
allscore.update(y) # to get data from array y
allscore.update(z) # to get data from array z
#stores all data in this array
如果要为每个用户存储多个值,可以使用tuples。
scores = {
"guv": (1, 2, 3),
"jane": (4, 5, 6),
"luiz": (7, 8, 9),
}
然后您可以使用以下循环获取所有用户的平均值:
for name in scores:
average = sum(scores[name]) / len(scores[name])
print("Average for " + name + ": " + str(average))
如果您希望能够添加分数,请改用 lists。
按字母顺序打印分数:
for name in sorted(scores):
print("Scores for " + name + ": " + str(scores[name]))
用于打印从最高平均分到最低平均分的分数:
averages = {
name: sum(scores[name]) / len(scores[name])
for name in scores.keys()
}
for name in sorted(averages, key=averages.get, reverse=True):
print("Average score for " + name + ": " + str(averages[name]))
如何将我的程序的多个结果存储在一个数组中?
对于数组中的每个人,我需要存储 3 个结果,但每次我这样做时,当程序为 运行.
程序为运行时,我找不到3个数组中每个人的平均分。
我是 PYTHON 的新手。
import operator
print("if form1 enter x, if form2 enter y,if form 3 enter z")
print("to view all results enter allscore")
x={"guv":1,"jane":4,"adam":5,"luiz":8,"elizabeth":9,"paul":10,"guv":3}
sorted(x.items(), key=operator.itemgetter(1), reverse=True) #sorts x from high to low
sorted(x.items()) #sorts x alphabetically
y={"adrian":0,"anna":9,"david":7,"matt":10,"micheal":5}
sorted(y.items(), key=operator.itemgetter(1), reverse=True)
sorted(y.items())
z={"gus":6,"mohammed":6,"winston":9,"barry":9,"ping":3}
sorted(z.items(), key=operator.itemgetter(1), reverse=True)
sorted(z.items()) #
allscore={}
allscore.update(x) # to get data from array x
allscore.update(y) # to get data from array y
allscore.update(z) # to get data from array z
#stores all data in this array
如果要为每个用户存储多个值,可以使用tuples。
scores = {
"guv": (1, 2, 3),
"jane": (4, 5, 6),
"luiz": (7, 8, 9),
}
然后您可以使用以下循环获取所有用户的平均值:
for name in scores:
average = sum(scores[name]) / len(scores[name])
print("Average for " + name + ": " + str(average))
如果您希望能够添加分数,请改用 lists。
按字母顺序打印分数:
for name in sorted(scores):
print("Scores for " + name + ": " + str(scores[name]))
用于打印从最高平均分到最低平均分的分数:
averages = {
name: sum(scores[name]) / len(scores[name])
for name in scores.keys()
}
for name in sorted(averages, key=averages.get, reverse=True):
print("Average score for " + name + ": " + str(averages[name]))