集合的计数器是否将数据排序?

发布于 2025-01-28 22:20:22 字数 478 浏览 2 评论 0 原文

我正在阅读Python Collections的 counter 。它说以下说明:

>>> from collections import Counter
>>> Counter({'z': 9,'a':4, 'c':2, 'b':8, 'y':2, 'v':2})
Counter({'z': 9, 'b': 8, 'a': 4, 'c': 2, 'y': 2, 'v': 2})

这些印刷值以降序(9> 8> 4> 2)以某种方式打印。为什么是这样? 计数器存储值排序吗?

PS:AM上的Python 3.7.7

I was reading python collections's Counter. It says following:

>>> from collections import Counter
>>> Counter({'z': 9,'a':4, 'c':2, 'b':8, 'y':2, 'v':2})
Counter({'z': 9, 'b': 8, 'a': 4, 'c': 2, 'y': 2, 'v': 2})

Somehow these printed values are printed in descending order (9 > 8 > 4 > 2). Why is it so? Does Counter store values sorted?

PS: Am on python 3.7.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

赢得她心 2025-02-04 22:20:23

counter 对象中存储的数据而言:从Python 3.7开始插入数据,因为 Counter 是内置 dict的子类。在Python 3.7之前,没有保证数据的顺序。

但是,您看到的行为来自 counter .__ repl __ 。我们可以从 noreferrer“代码> 方法,该方法按值按降顺序按价值对。如果由于值不可分类而失败,则它将落回 dict 表示形式,同样,该表示将插入。

In terms of the data stored in a Counter object: The data is insertion-ordered as of Python 3.7, because Counter is a subclass of the built-in dict. Prior to Python 3.7, there was no guaranteed order of the data.

However, the behavior you are seeing is coming from Counter.__repr__. We can see from the source code that it will first try to display using the Counter.most_common method, which sorts by value in descending order. If that fails because the values are not sortable, it will fall back to the dict representation, which, again, is insertion-ordered.

无风消散 2025-02-04 22:20:23

顺序取决于python版本

python< 3.7,没有保证的订单,因为Python 3.7订单是插入的订单。

在版本3.7中更改:作为一个dict子类,计数器继承了
记住插入顺序的能力。计数器的数学操作
对象也保留顺序。结果是根据
元素首先在左操作数中遇到,然后在顺序中遇到
在正确的操作数中遇到。

Python 3.8(3.8.10 [GCC 9.4.0])上的示例:

from collections import Counter
Counter({'z': 9,'a':4, 'c':2, 'b':8, 'y':2, 'v':2})

输出:

Counter({'z': 9, 'a': 4, 'c': 2, 'b': 8, 'y': 2, 'v': 2})

如何检查该 Counter

Counter中不计数为 __ str __ __ 不按数量进行排序。 返回 most_common ,它不是检查订单的可靠方法。

转换为 dict __ str __ 表示表示是忠实的。

c = Counter({'z': 9,'a':4, 'c':2, 'b':8, 'y':2, 'v':2})

print(dict(c))
# {'z': 9, 'a': 4, 'c': 2, 'b': 8, 'y': 2, 'v': 2}

The order depends on the python version.

For python < 3.7, there is no guaranteed order, since python 3.7 the order is that of insertion.

Changed in version 3.7: As a dict subclass, Counter inherited the
capability to remember insertion order. Math operations on Counter
objects also preserve order. Results are ordered according to when an
element is first encountered in the left operand and then by the order
encountered in the right operand.

Example on python 3.8 (3.8.10 [GCC 9.4.0]):

from collections import Counter
Counter({'z': 9,'a':4, 'c':2, 'b':8, 'y':2, 'v':2})

Output:

Counter({'z': 9, 'a': 4, 'c': 2, 'b': 8, 'y': 2, 'v': 2})

how to check that Counter doesn't sort by count

As __str__ in Counter return the most_common, it is not a reliable way to check the order.

Convert to dict, the __str__ representation will be faithful.

c = Counter({'z': 9,'a':4, 'c':2, 'b':8, 'y':2, 'v':2})

print(dict(c))
# {'z': 9, 'a': 4, 'c': 2, 'b': 8, 'y': 2, 'v': 2}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文