python中最好的有序的dict实现是什么? [英] What is the best ordered dict implementation in python?

查看:330
本文介绍了python中最好的有序的dict实现是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过(并写了)一些这样的实现。有没有被认为是最好的或正在成为一个标准?

I've seen (and written) a number of implementations of this. Is there one that is considered the best or is emerging as a standard?

我的意思是有序的dict是对象有一些概念的键的顺序它类似于PHP中的数组。

What I mean by ordered dict is that the object has some concept of the order of the keys in it, similar to an array in PHP.

odict from PEP 372 似乎是一个强大的候选人,但并不完全清楚它是赢家。

odict from PEP 372 seems like a strong candidate, but it's not totally clear that it is the winner.

推荐答案

这个由Raymond Hettinger创建的这个代码是对集合的替代.OrderedDict将出现在Python 2.7中: http://pypi.python.org/pypi/ordereddict

This one by Raymond Hettinger is a drop-in substitute for the collections.OrderedDict that will appear in Python 2.7: http://pypi.python.org/pypi/ordereddict

开发版本的集合文档说它相当于Python中的内容2.7,所以很可能是平滑过渡到Python将会顺利过渡。

The dev version of the collections docs say it's equivalent to what will be in Python 2.7, so it's probably pretty likely to be a smooth transition to the one that will come with Python.

我把它放在PyPI中,所以你可以用 easy_install ordereddict 安装它,并像它一样使用:

I've put it in PyPI, so you can install it with easy_install ordereddict, and use it like so:

from ordereddict import OrderedDict
d = OrderedDict([("one", 1), ("two", 2)])

这篇关于python中最好的有序的dict实现是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆