在Python 3中查找列表中字符串的平均值
我有一个这样的分数列表:在Python 3中查找列表中字符串的平均值,python,python-3.x,Python,Python 3.x,我有一个这样的分数列表: scores = ["Bob Foo - 10 / 20\n", "Jim Foo - 5 / 20\n", "Bob Foo - 7 / 20\n", "Jack T - 6 / 20\n", "Jack T - 4 / 20\n", "Bob Foo - 9 / 20\n"] 我需要尝试
scores = ["Bob Foo - 10 / 20\n",
"Jim Foo - 5 / 20\n",
"Bob Foo - 7 / 20\n",
"Jack T - 6 / 20\n",
"Jack T - 4 / 20\n",
"Bob Foo - 9 / 20\n"]
我需要尝试找到每个人的平均分数,分别四舍五入到最接近的整数。该列表可能会改变其规模和名称,但其格式始终为:
名字姓氏-得分/20
我想要一个类似以下内容的输出:
>>> Bob Foo - 9
Jim Foo - 5
Jack T - 5
我真的不知道在这里该怎么办。我知道排序函数以及如何指定键,但我不知道这是否有帮助
我得到的最接近的结果是:
for score in scores:
print(re.split(r'[-/]',score))
这一点都不接近,因为它所做的只是将列表拆分,并给我以下信息:
>>>
['Bob Foo ', ' 10 ', ' 20\n']
['Jim Foo ', ' 5 ', ' 20\n']
['Bob Foo ', ' 7 ', ' 20\n']
['Jack T ', ' 6 ', ' 20\n']
['Jack T ', ' 4 ', ' 20\n']
['Bob Foo ', ' 9 ', ' 20\n']
>>>
我怎样才能克服这个问题?我可以创建一个函数来查找列表中每个人的平均分数吗
编辑:
我可以将分数列表简化为更简单的格式。例如:
scores = ["Bob Foo 10\n",
"Jim Foo 5\n",
"Bob Foo 7\n",
"Jack T 6\n",
"Jack T 4\n",
"Bob Foo 9\n"]
将按照您以前演示的方式分解数据,但它只是将数据拆分为名称、rest,现在您只需保存它
data = {}
for name,score in parts:
try:
data[name].append(score)
except KeyError:
data[name] = [score,]
print data
现在,您已经按名称对分数进行了分组,所以现在您只需将分数转换为实际的int
(或者更好的float
)
平均每个名字的分数我发现划分和征服编程任务很有用。将复杂任务划分为一系列琐碎的简单任务。要么你知道如何单独做每一件事,要么你可以在你正在挣扎的部分得到更有针对性的帮助 举个例子,我将如何解决这个问题:
import re
scores = ["Bob Foo - 10 / 10\n",
"John Smith - 5 / 10\n",
"Bob Foo - 7 / 10\n",
"Jack T - 6 / 10\n",
"Jack T - 4 / 10\n",
"Bob Foo - 9 / 10\n"]
# First, split the names and scores up
scores = [re.match('(\S+ \S+) - (\d+)', score).groups() for score in scores]
#print (scores)
# Convert the number string into an integer
scores = [[score[0], int(score[1])] for score in scores]
#print (scores)
# Create a dictionary, keyed by the name
names = set(score[0] for score in scores)
scores = {
name: [score[1] for score in scores if name == score[0]]
for name in names
}
# print (scores)
# Compute the average for each name
scores = {
name: sum(score)/len(score)
for name, score in scores.items()
}
print (scores)
试试这个,可能对你有帮助
import math
scores = ["Bob Foo - 10 / 10\n",
"John Smith - 5 / 10\n",
"Bob Foo - 7 / 10\n",
"Jack T - 6 / 10\n",
"Jack T - 4 / 10\n",
"Bob Foo - 9 / 10\n"]
d={}
for i in xrange(len(scores)):
str=scores[i].split("-");
try:
(d[str[0]])[0] +=int((str[1].split("/"))[0])
(d[str[0]])[1] +=1
except:
d[str[0]]=[0,0]
(d[str[0]])[0]=int((str[1].split("/"))[0])
(d[str[0]])[1]=1
//store sum and count in a dictionary
for x in d:
print x,((d[x])[0]/((d[x])[1])*1.0)
在最后一句话中,使用适当的功能天花板或地板。或圆形
也许你应该考虑一种更合理的存储格式开始?@jordan是的,我可以将其简化为:Bob Foo 10不,我没有看到多个条目。。。只是第一个。。。它总是在10点之外吗?平均值不应该是一个百分比吗?是的,分数总是在10分之外,我可以让它成为一个百分比,这很好。我尝试了这段代码,但我得到了一个AttributeError:'str'对象没有数据[name]的属性'append'。append(score)我正在使用python 3。谢谢,但我得到了一个奇怪的输出(对我来说)我对字典不是很在行,所以,你能举个例子,说明我如何将分数转换成实际的整数,并对每个名字的分数求平均值吗?这很好,而且很有效,但是我现在如何对平均值进行四舍五入呢?例如,Bob Foo的平均值为8.6,我希望9您可以使用内置函数。
import math
scores = ["Bob Foo - 10 / 10\n",
"John Smith - 5 / 10\n",
"Bob Foo - 7 / 10\n",
"Jack T - 6 / 10\n",
"Jack T - 4 / 10\n",
"Bob Foo - 9 / 10\n"]
d={}
for i in xrange(len(scores)):
str=scores[i].split("-");
try:
(d[str[0]])[0] +=int((str[1].split("/"))[0])
(d[str[0]])[1] +=1
except:
d[str[0]]=[0,0]
(d[str[0]])[0]=int((str[1].split("/"))[0])
(d[str[0]])[1]=1
//store sum and count in a dictionary
for x in d:
print x,((d[x])[0]/((d[x])[1])*1.0)