77家的会客2010

Python下为字典排序
Weather:阴 ,东南风 4-5级 ,最低气温4 ℃

<p># (IMHO) the simplest approach:<br /> def sortedDictValues1(adict):<br /> &nbsp;&nbsp;&nbsp; items = adict.items()<br /> &nbsp;&nbsp;&nbsp; items.sort()<br /> &nbsp;&nbsp;&nbsp; return [value for key, value in items]<br /> <br /> # an alternative implementation, which<br /> # happens to run a bit faster for large<br /> # dictionaries on my machine:<br /> def sortedDictValues2(adict):<br /> &nbsp;&nbsp;&nbsp; keys = adict.keys()<br /> &nbsp;&nbsp;&nbsp; keys.sort()<br /> &nbsp;&nbsp;&nbsp; return [dict[key] for key in keys]<br /> <br /> # a further slight speed-up on my box<br /> # is to map a bound-method:<br /> def sortedDictValues3(adict):<br /> &nbsp;&nbsp;&nbsp; keys = adict.keys()<br /> &nbsp;&nbsp;&nbsp; keys.sort()<br /> &nbsp;&nbsp;&nbsp; return map(adict.get, keys)</p>

[Python下为字典排序]的回复

Post a Comment~