找到每个玩家的得分数

时间:2014-12-06 12:27:42

标签: python python-3.x

我正在尝试添加每位玩家分配给他的得分项目数量,并发现每个玩家归因于他的得分数量存在问题。我的代码如下,但我似乎无法克服最后的障碍:

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

我基本上是想找到每个球员的平均得分。

1 个答案:

答案 0 :(得分:0)

来自collections.deque() documentation

  

除上述内容外,deques支持迭代,腌制, len(d) reversed(d)copy.copy(d)copy.deepcopy(d),使用in运算符和下标引用,例如d[-1]

强调我的

要获得长度,只需使用len(scores)

player_averages = {name: sum(scores) / len(scores) for name, scores in player_stats.items()}