BurntSushi / nflgame

An API to retrieve and read NFL Game Center JSON data. It can work with real-time data, which can be used for fantasy football.
http://pdoc.burntsushi.net/nflgame
The Unlicense
1.28k stars 413 forks source link

Sorting with multiple attributes #230

Open adityagore opened 8 years ago

adityagore commented 8 years ago

I want to sort receivers on two attributes, first by receiving_rec then by receiving_tar. The sort command as of now only takes one string argument. Is there a way to do something like

players.receiving().sort(['receiving_rec','receiving_tar'])

ochawkeye commented 8 years ago

I think you'll have to rely more on Python than the built-in convenience functions that nflgame offers.

import nflgame

games = nflgame.games(2015, kind='REG')

players = nflgame.combine_max_stats(games)
players = sorted(players, key=lambda x: (x.receiving_rec, x.receiving_tar), reverse=True)
for player in players[:10]:
    print player.player, player.receiving_rec, player.receiving_tar
Julio Jones (WR, ATL) 136 203
Antonio Brown (WR, PIT) 136 193
DeAndre Hopkins (WR, HOU) 111 191
Jarvis Landry (WR, MIA) 111 167
Brandon Marshall (WR, NYJ) 109 173
Larry Fitzgerald (WR, ARI) 109 146
Demaryius Thomas (WR, DEN) 105 177
Odell Beckham (WR, NYG) 96 158
Delanie Walker (TE, TEN) 94 133
Golden Tate (WR, DET) 90 129