Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 sum a dictionary of classes
Hi there,

I have a dictionary of classes. The class consists of data for soccer games with attributes like team, league, lists of results, lists of calculated values (floats). I want to get the sum (or the average) of these calculated values. I can easily get these sums per team because the dictionary is "keyed" by team, doing like this:

    for team in laglista:
        "\t", bibliotek_lag[team].liga,
        "\t", str(round(stats.gmean(bibliotek_lag[team].hit_percentage),2))
However, i also want to be able to get the sum per legaue or any other attribute, not just by team.

I can sort the dictionary like this, but i cant figure out how get the sum of match.hit_percentage for example by league?

for match in (sorted(bibliotek_lag.values(), key=operator.attrgetter('liga'))):
   print(match.liga, match.lag, str(round(stats.gmean(match.hit_percentage),2))
I might be way off here, i have a feeling there must be a much better way to do this...

Cheers :)
The code shown at the moment has a lot of unknowns.
Post as much code as necessary and as little as possible to enable the code to be run.
what is the input values what is the expected output.
Ah yeah sorry, i can see that now. But let's put it like this. I have a chunk of data (a list of list) and i want to get the sum of certain columns (Mean and hit) sorted by keys (Team, League, Odds group):

The data:
Team League Odds group Mean Hit
Arsenal PL 1 0,4 0,5
Fulham PL 3 0,25 0,4
Aston Villa CL 2 0,55 0,6
Sunderland L1 1 0,43 0,5
Arsenal PL 1 0,3 0,4
Fulham PL 2 0,5 0,5
Aston Villa CL 1 0,4 0,5
Sunderland L1 1 0,4 0,6

The sum sorted by team
Sum by team Mean Hit
Arsenal 0,7 0,9
Fulham 0,75 0,9
Aston Villa 0,95 1,1
Sunderland 0,83 1,1

The sum sorted by league
Sum by league Mean Hit
PL 1,45 1,8
PL 1,45 1,8
CL 0,95 1,1

The sum sorted by odds group
Sum by odds group Mean Hit
1 1,93 2,5
2 1,05 1,1
3 0,25 0,4

Is there a clever way to accomplish this? Maybe use a dictionary or a class or something else?
Hope this makes the question more clear :)

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Using classes? Can I just use classes to structure code? muteboy 5 1,614 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

Users browsing this thread: 1 Guest(s)