0

I understand that dictionary is not ordered. I know that.

However, this is a code of counting word frequency.

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]))

I called the wordCount with the following string.

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

This is the result.

Run #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.

Run #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.

I understand that is not ordered, but even if they are not ordered, why the order is different? My imagination is the reason it's not alphabetically ordered because the order is based on the time they are registered (ie.queue)

I can't imagine that they call random.shuffle() when I want to display it.

Realdeo
  • 449
  • 6
  • 19
  • 1
    Next time, try pasting your question's title into a Google search before posting it. You can often get useful results like this. – TigerhawkT3 Oct 31 '16 at 04:21

2 Answers2

0

Python's hash function is seeded with a random number generator each time its run, this is to prevent DDoS attacks, since a malicious adversary could create specially crafted inputs that would make dictionary operations happen in O(n) by generating a lot of hash collision.

You can read more about it here

Francisco
  • 10,918
  • 6
  • 34
  • 45
-1

Many dictionary (a.k.a. map) implementations are based on the hash table data structure for very fast retrieval. This means that keys are hashed to some random-looking index, and values are placed in those slots. When iterating over a dictionary, it is easiest to walk the array in order, which means the order corresponds to the hash order of the keys.

As for why the order might differ from run to run, there are two good reasons:

  1. The capacity of the hash table might differ due to different defaults, different history, etc. Because of this, the hash order will be different when the size of the table is different.

  2. The hash order might be randomized deliberately to prevent attacks. When the hash function is fixed and known, an attacker can try to put many items into the same bucket, causing the hash table to slow down to the speed of a linked list. See also: Why is dictionary ordering non-deterministic?

Community
  • 1
  • 1
Nayuki
  • 17,911
  • 6
  • 53
  • 80