python中最好的有序字典实现是什么?

发布于 2024-07-16 08:16:21 字数 226 浏览 10 评论 0原文

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

我所说的有序 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

焚却相思 2024-07-23 08:16:21

Raymond Hettinger 的这个是 Python 2.7 中将出现的 collections.OrderedDict 的直接替代品: http: //pypi.python.org/pypi/ordereddict

集合文档的开发版本表示它相当于 Python 2.7 中的内容,因此很可能会平滑过渡到随之而来的版本Python。

我已将其放入 PyPI 中,因此您可以使用 easy_installordereddict 安装它,并像这样使用它:

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

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:

from ordereddict import OrderedDict
d = OrderedDict([("one", 1), ("two", 2)])
柠檬 2024-07-23 08:16:21

我还没有看到一个标准; 每个人似乎都推出了自己的(请参阅 这个问题)。 如果您可以使用 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.

满栀 2024-07-23 08:16:21

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

然而它的实现方法是简约的,如果这还不够,你应该看看 odict通过 Foord/Larossa 或 ordereddict (由我),在这种情况下,这些是更好的合身。 两种实现都是collections.OrderedDict 提供的功能的超集。 两者之间的区别在于,odict 是纯 Python,而 ordereddict 是更快的 C 扩展模块。

即使它提供了您需要的所有功能,简约的方法也不一定更好:例如 collections.OrderedDict 最初确实有一个 错误 返回嵌套在其自身值之一的 OrderedDictrepr() 时。 如果使用了旧 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, that odict is pure python and ordereddict a much faster C 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 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.

孤独难免 2024-07-23 08:16:21

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文