-
- Python下为字典排序
- Weather:阴 ,东南风 4-5级 ,最低气温4 ℃
- 2006-04-03
<p># (IMHO) the simplest approach:<br /> def sortedDictValues1(adict):<br /> items = adict.items()<br /> items.sort()<br /> 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 /> keys = adict.keys()<br /> keys.sort()<br /> 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 /> keys = adict.keys()<br /> keys.sort()<br /> return map(adict.get, keys)</p>
-
Views(5335) | Comments(0) |
In:
Python相关
|
(03/30)
