python以什么顺序显示字典的键

2024-09-28 21:01:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道那本字典没有订。我知道

然而,这是一个计算词频的代码

def wordCount(kalimat):
    counter = {}
    for kata in kalimat.split(" "):
        if kata in counter:
            counter[kata] += 1
        else:
            counter[kata] = 1
    for I in sorted(counter):
        if counter[I] == 1:
            print("{:<10} appears 1   time.".format(I,counter[I]))
        else:
            print("{:<10} appears {:<3} times.".format(I,counter[I]))

我用下面的字符串调用了wordCount

A word may appear once but twice may appear twice since this one will not appear again with this animal

这就是结果

运行#1

again      appears 1   time.
not        appears 1   time.
one        appears 1   time.
may        appears 2   times.
word       appears 1   time.
appear     appears 3   times.
since      appears 1   time.
twice      appears 2   times.
but        appears 1   time.
with       appears 1   time.
will       appears 1   time.
A          appears 1   time.
animal     appears 1   time.
this       appears 2   times.
once       appears 1   time.

运行#2

once       appears 1   time.
word       appears 1   time.
will       appears 1   time.
animal     appears 1   time.
appear     appears 3   times.
again      appears 1   time.
A          appears 1   time.
not        appears 1   time.
one        appears 1   time.
but        appears 1   time.
twice      appears 2   times.
may        appears 2   times.
with       appears 1   time.
since      appears 1   time.
this       appears 2   times.

我明白这不是命令,但即使他们没有命令,为什么命令是不同的?我的想象力是它不按字母顺序排列的原因,因为顺序是基于它们注册的时间(即队列)

我无法想象当我想显示它时他们会调用random.shuffle()


Tags: intimecounterthisonemaywordbut
2条回答

许多dictionary(又称map)实现都基于hash table数据结构,以实现非常快速的检索。这意味着键被散列到一些看起来随机的索引中,值被放置在这些槽中。在对字典进行迭代时,最容易按顺序遍历数组,这意味着顺序对应于键的哈希顺序

至于为什么顺序可能不同,有两个很好的理由:

  1. 由于不同的默认值、不同的历史记录等,哈希表的容量可能不同。因此,当表的大小不同时,哈希顺序也会不同

  2. 散列顺序可能会被故意随机分配以防止攻击。当哈希函数是固定的且已知时,攻击者可以尝试将多个项目放入同一个存储桶,从而导致哈希表的速度降低到链表的速度。另请参见:Why is dictionary ordering non-deterministic?

Python的hash函数每次运行时都会有一个随机数生成器作为种子,这是为了防止DDoS攻击,因为恶意的对手可以通过生成大量的hash冲突来创建巧尽心思构建的输入,从而使字典操作在O(n)中发生

你可以阅读更多关于它here

相关问题 更多 >