登录 注册

Python中最快的字典排序方法

收藏
[Python] 标签:dict排序字典排序 2012-10-28 17:49
来自PEP265,号称最快的字典排序方法: http://www.python.org/dev/peps/pep-0265/
这里还有篇相关博客:http://www.kunli.info/2009/05/07/sorting-dictionaries-by-value-in-python/
原始代码 全屏查看 0评 / 0藏 / 4716阅  跳至 / 4行
1
2
3
4
# tags的数据结构如:{'苹果':1,'香蕉':3,...}
from operator import itemgetter
result = sorted(tags.iteritems(), key=itemgetter(1), reverse=True)
# result:[('香蕉',3),('苹果',1),...]

最新评论

  · · · · · ·  (共0条)

目前还没有评论

登录后您才可以发表评论。 马上登录 立即注册
cottage
2012-02-29加入
一段精湛的代码,天涯何处寻知音!
Back to Top