Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sum a dictionary of classes
#1
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:
        print(team(25),
        "\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 :)
Reply


Messages In This Thread
sum a dictionary of classes - by tobbs - Mar-15-2019, 04:54 PM
RE: sum a dictionary of classes - by Yoriz - Mar-15-2019, 05:28 PM
RE: sum a dictionary of classes - by tobbs - Mar-16-2019, 02:38 PM

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

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020