0

I am getting an error message when trying to add the values for each player from a dictionary, it could be because the values in my dictionary is a string but I am not sure. The code is:

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(score)
        sum(player_stats.values())

print(player_stats)

The data file is:

rooney 12
rooney 23
rooney 56
rooney 27
ronaldo 14
ronaldo 34
messi 23
messi 45
messi 12
messi 56
Dave J
  • 475
  • 9
  • 18

1 Answers1

0

You are correct, the values in your dict are strings instead of ints. In this block of code

for line in f:
    name, score = line.split()
    player_stats.setdefault(name, deque(maxlen=3))
    player_stats[name].append(score) # this line
    sum(player_stats.values())

change the marked line to

player_stats[name].append(int(score))

and you should be all set.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • still get an error- values in a dictionary.py", line 16, in sum(player_stats.values()) TypeError: unsupported operand type(s) for +: 'int' and 'collections.deque' – Darth Sidius Dec 02 '14 at 19:04
  • @DarthSidius why are you using a deque instead of a list? – MattDMo Dec 02 '14 at 19:06
  • it was the easiest way to only get the most recent three entries for each player. Or should I say the only way I could understand... – Darth Sidius Dec 02 '14 at 19:07
  • @DarthSidius Oh, I didn't realize that was a requirement. In that case, you'll need to write a custom `calculate_sum()` function that iterates over each item and adds them together, as `deque` does not have an `__add__()` method, unlike lists. – MattDMo Dec 02 '14 at 19:16
  • how would I do that MattDMo – Darth Sidius Dec 02 '14 at 19:21
  • @DarthSidius here's another idea - use a list instead of a deque, then shorten the list to 3 items before summing. – MattDMo Dec 02 '14 at 19:21
  • point me in the right direction on how to shorten a list to three items fella. Thanks – Darth Sidius Dec 02 '14 at 19:31
  • @DarthSidius just use [slicing](http://stackoverflow.com/q/509211/1426065) - `mylist[:2]` will give you the first 3 items (remember indexes are 0-based in Python), while `mylist[-3:]` will give you the last 3 elements. – MattDMo Dec 02 '14 at 19:34
  • yes but how do I get the three most recent entries for each player, have a look at my data file above – Darth Sidius Dec 02 '14 at 19:36
  • @DarthSidius since lists are ordered, you know that `mylist[0]` was entered 1st, `mylist[1]` was entered 2nd, and so on. Think about that, and decide if you want the first 3 elements or the last 3. – MattDMo Dec 02 '14 at 19:43
  • okay so I have a list players[] with many players and their respective scores. Each player will have many scores entered but I only want their three most recent entries. therefore I use [-3:] but I want it to return the three recent entries for each player, that's the bit that im having trouble with... – Darth Sidius Dec 02 '14 at 19:49