我正在尝试添加每个玩家分配给他的分数项的数量,并且发现返回每个玩家分配给他的分数是有问题的。我的代码如下,但我似乎无法跨越最后的障碍:
with open("players.dat") as f:
group = []
for line in f:
fields = line.split()
group.append( (fields[0], int(fields[1])))
print(group)
from collections import deque
player_stats = {}
with open("players.dat") as f:
for line in f:
name, score = line.split()
player_stats.setdefault(name, deque(maxlen=3))
player_stats[name].append(int(score))
print(player_stats)
print(len(score))
player_totals = {name: sum(scores) for name, scores in player_stats.items()}
print(player_totals)
player_totals = {name: max(scores) for name, scores in player_stats.items()}
print(player_totals)
player_totals = {name: min(scores) for name, scores in player_stats.items()}
print(player_totals)
#testing counting number of scores for each player
#from collections import Counter
#items = Counter(val[1] for val in player_stats.values())
#print(items)
player_totals = {name:len(score)}
print(player_totals)
我的数据文件是:
rooney 12
rooney 23
rooney 56
rooney 27
ronaldo 14
ronaldo 34
messi 23
messi 45
messi 12
messi 56
我基本上是想找出每个球员的平均得分。你知道吗
从^{} documentation :
强调我的。你知道吗
要获得长度,只需使用
len(scores)
:相关问题 更多 >
编程相关推荐