python中最好的有序字典实现是什么?
我已经看到(并写过)许多这样的实现。 是否有一个被认为是最好的或正在成为标准的?
我所说的有序 dict 是指对象有一些关于其中键的顺序的概念,类似于 PHP 中的数组。
来自 PEP 372 的命令似乎是一个强有力的候选者,但尚不完全清楚它是赢家。
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?
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 seems like a strong candidate, but it's not totally clear that it is the winner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Raymond Hettinger 的这个是 Python 2.7 中将出现的 collections.OrderedDict 的直接替代品: http: //pypi.python.org/pypi/ordereddict
集合文档的开发版本表示它相当于 Python 2.7 中的内容,因此很可能会平滑过渡到随之而来的版本Python。
我已将其放入 PyPI 中,因此您可以使用
easy_installordereddict
安装它,并像这样使用它: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
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.
I've put it in PyPI, so you can install it with
easy_install ordereddict
, and use it like so:我还没有看到一个标准; 每个人似乎都推出了自己的(请参阅 这个问题)。 如果您可以使用 PEP 372 中的
OrderedDict
补丁,那么这是您最好的选择。 stdlib 中包含的任何内容很有可能在一两年后成为每个人都使用的内容。I haven't seen a standard; everyone seems to roll their own (see answers to this question). If you can use the
OrderedDict
patch from PEP 372, that's your best bet. Anything that's included in the stdlib has a very high chance of being what everyone uses a year or two from now.Python 2.7 及更高版本在
collections
模块中包含 OrderedDict,因此您应该将其视为“标准”。 如果它的功能足够,您可能应该使用它。然而它的实现方法是简约的,如果这还不够,你应该看看 odict通过 Foord/Larossa 或 ordereddict (由我),在这种情况下,这些是更好的合身。 两种实现都是
collections.OrderedDict
提供的功能的超集。 两者之间的区别在于,odict
是纯 Python,而ordereddict
是更快的C
扩展模块。即使它提供了您需要的所有功能,简约的方法也不一定更好:例如
collections.OrderedDict
最初确实有一个 错误 返回嵌套在其自身值之一的OrderedDict
的repr()
时。 如果使用了旧ordereddict
的单元测试的子集(OrderedDict 可以处理的小子集),则可能会更早发现该错误。Python 2.7 and later have OrderedDict in the
collections
module, so you should consider that as 'standard'. If its functionality is enough you should probably be using that.However its implementation approach is minimalistic and if that is not enough you should look at odict by Foord/Larossa or ordereddict (by me) as in that case those are a better fit. Both implementations are a superset of the functionality provided by
collections.OrderedDict
. The difference between the two being, thatodict
is pure python andordereddict
a much fasterC
extension module.A minimalistic approach is not necessarily better even if it provides all the functionality you need: e.g.
collections.OrderedDict
did initially have a bug when returning therepr()
of aOrderedDict
nested in one of its own values. A bug that could have been found earlier, had the subset, the small subset OrderedDict can handle, of unittests of the olderordereddict
been used.collections.OrderedDict
现在应该可以广泛使用,但如果担心性能,您可以考虑使用我的包 cyordereddict 作为替代方案。 它是标准库的 OrderedDict 到 Cython 的直接移植,速度提高了 2-6 倍。collections.OrderedDict
should now be widely available, but if performance is concern, you might consider using my package cyordereddict as an alternative. It's a direct port of standard library's OrderedDict to Cython that is 2-6x faster.