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

时间:2016-10-31 04:13:07

标签: python dictionary

我明白字典不是有序的。我知道。

但是,这是一个计算单词频率的代码。

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。

  

一个单词可能会出现一次,但两次可能出现两次,因为这个单词不会再出现在这个动物身上

这是结果。

运行#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.

我明白这不是有序的,但即使没有订购,为什么订单不同?我的想象力是不按字母顺序排序的原因,因为订单是基于他们注册的时间(即.queue)

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

2 个答案:

答案 0 :(得分:0)

Python的哈希函数在每次运行时都会使用随机数生成器播种,这是为了防止DDoS攻击,因为恶意攻击者可以创建特制的输入,通过生成很多内容来使O(n)中的字典操作发生哈希冲突。

您可以阅读更多相关信息here

答案 1 :(得分:-1)

许多字典(a.k.a.map)实现基于hash table数据结构,可以快速检索。这意味着将密钥散列到一些随机索引,并将值放在这些插槽中。迭代字典时,按顺序遍历数组最简单,这意味着顺序对应于键的哈希顺序。

至于为什么订单可能因运行而异,有两个很好的理由:

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

  2. 哈希顺序可能会被故意随机化以防止攻击。当哈希函数被修复并且已知时,攻击者可以尝试将许多项放入同一个桶中,从而使哈希表减慢到链表的速度。另见:Why is dictionary ordering non-deterministic?