>>> from collections import *
>>> stats = {'Game':0,'Player':'foo','Tds':1}
>>> for key in stats: print key
...
Tds
Player
Game
>>> ostats = OrderedDict([('Game',0),('Player','foo'),('Tds',1)])
>>> for okey in ostats: print okey
...
Game
Player
Tds
Sunday, September 11, 2011
Python OrderedDict
Just a Python programming tip for those of us who are new to the language. When I started writing the code to extract the stats and insert into sql I discovered that the dict class in Python does not maintain the order of the keys. This means I cannot loop over a dictionary of stats to insert into different player classes (offense, kickers, and defense). I discovered OrderedDict, which does keep the order. Very useful for making your code cleaner. Here is an example of the differences running in the Python shell:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment