我试图创建一个从二维数组中获取部分的函数,对它们求平均值,然后将其打印出来。
数组看起来像这样:
data = [['Name:','John','Score:','6'],['Name:','John','Score:','8'],['Name:','John','Score:','6']]
如何获得分数的平均值?
编辑1:
def average():
print("\n\nAverage Scores: ")
length = len(data)
ind = 0
data.sort(key=operator.itemgetter(1))
sav2 = []
while length != 0:
name = data[ind][1]
counted = sum(x.count(name) for x in log)
counting = counted
av = 0
while counting != 0:
av += int(data[ind][3])
ind += 1
counting -= 1
av = str(av/counted)
sav2.append(("Name: "+name+" Average Score: "+av).split())
length -= int(counted)
sav2.sort(key=lambda x: float(x[4]), reverse = True)
for word in sav2:
wordJoin = ' '.join(word)
print(wordJoin)
这是我从朋友那里借来的一些代码,它工作正常,但我不明白。你们有什么可以做的吗?
答案 0 :(得分:1)
你应该向其他人展示你的任何试验。另外,正如他在评论中提到的tobias_k,你应该使用字典。
快速回答您的问题:
summation, count = 0, 0
for i in data:
summation += float(i[3])
count += 1
averageResult = summation / count
答案 1 :(得分:0)
对于这个具体的例子:
sum([int(i[3]) for i in data])/3.0
而不是3.0
您可以使用float(len(data))
如果数组中的数字不是整数,那么:
sum([float(i[3]) for i in data])/len(data)
答案 2 :(得分:0)
您现在可以找到平均值
>>>sum([int(i[-1]) for i in data])/float(len(data))
6.666666
答案 3 :(得分:0)
根据当前数据
collection
模块。演示:
>>> import collections
>>> student_record = collections.defaultdict(list)
>>> data = [['Name:','John','Score:','6'],['Name:','John','Score:','8'],['Name:','John','Score:','6']]
>>> for i in data:
... student_record[i[1]].append(int(i[3]))
...
>>> student_record
defaultdict(<type 'list'>, {'John': [6, 8, 6]})
>>> for i,j in student_record.items():
... print "Name: %s, Score: %s, Avg: %s"%(i, j, float(sum(j))/len(j))
...
Name: John, Score: [6, 8, 6], Avg: 6.66666666667
>>>
将输入结构更改为列表字典。
>>> import collections
>>> student_record = collections.defaultdict(list)
>>> data = [{'Name':'John','Score':'6'},{'Name':'John','Score':'8'},{'Name':'John','Score':'6'}]
>>> for i in data:
... student_record[i["Name"]].append(int(i["Score"]))
...
>>> student_record
defaultdict(<type 'list'>, {'John': [6, 8, 6]})
>>> for i,j in student_record.items():
... print "Name: %s, Score: %s, Avg: %s"%(i, j, float(sum(j))/len(j))
...
Name: John, Score: [6, 8, 6], Avg: 6.66666666667
答案 4 :(得分:0)
我认为最好以尽可能广泛的解决方案来解决这些问题,因为它最能说明它背后的概念。考虑到这一点:
我们需要一个采用2D数组并返回特定列的平均值的脚本。因此,我们的函数应该采用2D数组和列索引
def averageOfColumn(people, columnIndex):
#code
return answer;
均值定义为总和除以n,因此我们希望找到一列的总和,然后除以人数。列的总和可以在
中找到answer = 0
for person in people:
answer += person[columnIndex]
如果我们将此总和除以人数,我们就得到了答案
def averageOfColumn(people, columnIndex):
answer = 0;
for person in people:
answer += person[columnIndex];
return answer / len(people);
我们还没有相当完成,因为你的数据是作为字符串给出的,我们希望它作为一个浮点数或一个int(所以我们可以对它进行数学运算),所以我们必须做稍微改动并将'数据'转换为数字类型
def averageOfStatistic(people, columnIndex):
summation = 0;
for person in people:
summation += float(person[columnIndex]);
return summation / len(people);
或者,更通用的术语:
def averageOfStatistic(data, columnIndex):
summation = 0;
for point in data:
summation += float(point[columnIndex]);
return summation / len(data);
所以我们终于有了
def averageOfStatistic(data, columnIndex):
summation = 0;
for point in data:
summation += float(point[columnIndex]);
return summation / len(data);
data = [['Name:','John','Score:','6'],['Name:','John','Score:','8'],['Name:','John','Score:','6']]
print averageOfStatistic(data, 3); # print average score