Open ochawkeye opened 10 years ago
What I ended up doing is convert the key to a string that would be consistent across queries and pre-collected info that I might want to use later on.
from collections import defaultdict
import nfldb
def tally_points(db, y, w, ph):
q = nfldb.Query(db)
player_score = defaultdict(int)
player_metadata = {}
q.game(season_year=y, week=w, season_type=ph)
for pp in q.as_aggregate():
p_id = str(pp.player)
player_metadata[p_id] = [pp.player.team, pp.player.position]
player_score[p_id] += 1.0*pp.kicking_xpmade
q.play_player(kicking_fgm=1)
for pp in q.as_play_players():
p_id = str(pp.player)
player_metadata[p_id] = [pp.player.team, pp.player.position]
fg_length = getattr(pp, 'kicking_fgm_yds')
if fg_length < 40:
player_score[p_id] += 3.0
elif fg_length < 50:
player_score[p_id] += 4.0
else:
player_score[p_id] += 5.0
return player_score, player_metadata
if __name__ == '__main__':
db = nfldb.connect()
player_points, p_info = tally_points(db, 2014, 6, 'Regular')
for player in player_points:
if player == 'Dan Bailey (DAL, K)':
print type(player), player, player_points[player], p_info[player][0], p_info[player][1]
print '-'*25
for player in player_points:
if player == 'Nick Folk (NYJ, K)':
print type(player), player, player_points[player], p_info[player][0], p_info[player][1]
print '-'*25
for player in player_points:
if player == 'Graham Gano (CAR, K)':
print type(player), player, player_points[player], p_info[player][0], p_info[player][1]
<type 'str'> Dan Bailey (DAL, K) 15.0 DAL K
-------------------------
<type 'str'> Nick Folk (NYJ, K) 5.0 NYJ K
-------------------------
<type 'str'> Graham Gano (CAR, K) 14.0 CAR K
Doing something like the following results in having multiple "keys" in my points dictionary that are the same type and appear to be the same key (but aren't).
With Bailey kicking 3FGs & 3XP and Folk kicking 1FG & 1 XP I would expect the output to be
Is there a way to merge these
pp.player
objects like I am trying to do or should I be using something else as the key in myplayer_score
dictionary? If I use something else, can I still retain all of thepp.player
objects properties so that I could still access all of that player info from the object?gist