python中最有序的dict实现是什么?

时间:2021-11-14 18:09:24

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.

我所说的有序dict是指对象有一些键的顺序概念,类似于PHP中的数组。

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

来自PEP 372的冒险似乎是一个强有力的候选人,但它并不完全清楚它是赢家。

4 个解决方案

#1


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

Raymond Hettinger的这个版本是collection.OrderedDict的替代品,它将出现在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.

集合文档的dev版本说它与Python 2.7中的相同,所以很可能是平滑过渡到Python附带的版本。

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

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

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

#2


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.

我还没有看到标准;每个人似乎都自己动手(见这个问题的答案)。如果你可以使用PEP 372的OrderedDict补丁,这是你最好的选择。 stdlib中包含的任何东西很有可能成为每个人从现在开始使用一两年的东西。

#3


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.

collections.OrderedDict现在应该可以广泛使用,但如果考虑性能,你可以考虑使用我的包cyordereddict作为替代。它是标准库的OrderedDict到Cython的直接端口,速度提高了2-6倍。

#4


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.

Python 2.7及更高版本在集合模块中有OrderedDict,因此您应该将其视为“标准”。如果它的功能足够你应该使用它。

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, that odict is pure python and ordereddict a much faster C extension module.

然而,它的实现方法是极简主义的,如果这还不够,你应该看看Foord / Larossa或者orderdict(由我)的odict,因为在那种情况下那些更合适。这两个实现都是collections.OrderedDict提供的功能的超集。两者之间的区别在于,odict是纯python并且orderdict是一个更快的C扩展模块。

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 the repr() of a OrderedDict 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 older ordereddict been used.

即使它提供了您需要的所有功能,简约方法也不一定更好:例如collections.OrderedDict在返回嵌套在其自己的值中的OrderedDict的repr()时最初有错误。一个本来可以找到的错误,有一个子集,小子集OrderedDict可以处理,使用了旧有序指令的单元测试。

#1


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

Raymond Hettinger的这个版本是collection.OrderedDict的替代品,它将出现在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.

集合文档的dev版本说它与Python 2.7中的相同,所以很可能是平滑过渡到Python附带的版本。

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

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

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

#2


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.

我还没有看到标准;每个人似乎都自己动手(见这个问题的答案)。如果你可以使用PEP 372的OrderedDict补丁,这是你最好的选择。 stdlib中包含的任何东西很有可能成为每个人从现在开始使用一两年的东西。

#3


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.

collections.OrderedDict现在应该可以广泛使用,但如果考虑性能,你可以考虑使用我的包cyordereddict作为替代。它是标准库的OrderedDict到Cython的直接端口,速度提高了2-6倍。

#4


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.

Python 2.7及更高版本在集合模块中有OrderedDict,因此您应该将其视为“标准”。如果它的功能足够你应该使用它。

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, that odict is pure python and ordereddict a much faster C extension module.

然而,它的实现方法是极简主义的,如果这还不够,你应该看看Foord / Larossa或者orderdict(由我)的odict,因为在那种情况下那些更合适。这两个实现都是collections.OrderedDict提供的功能的超集。两者之间的区别在于,odict是纯python并且orderdict是一个更快的C扩展模块。

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 the repr() of a OrderedDict 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 older ordereddict been used.

即使它提供了您需要的所有功能,简约方法也不一定更好:例如collections.OrderedDict在返回嵌套在其自己的值中的OrderedDict的repr()时最初有错误。一个本来可以找到的错误,有一个子集,小子集OrderedDict可以处理,使用了旧有序指令的单元测试。