0

I've been searching everywhere for a problem that occurred in one of my codes, but didn't see any solution... It's been pretty annoying and that is why I'm asking you this question.

I'm using a simple dictionnary with keys and values, but the problem is when I want to print it at the end it has a strange shuffle where two letters are inverted, like for example "C":"W", "B":"X" instead of "B":"X", "C":"W". Here is my code (it will probably be clearer)

PS. I've first tried in the last few lines to replace the while structure by a for structure, with no improvement.

import random

m1 = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ"]

l1 = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ"]

class Separate:
#Used to separate objects in a list
    def __init__(self, d):
            self.d = d

            self.d = "".join(self.d)
            l = []
            for elt in range(0, len(self.d)):
                    l.append(self.d[elt])
            self.d = l

l1 = Separate(l1)
l1 = l1.d
m1 = Separate(m1)
m1 = m1.d

random.shuffle(m1)

key = {}

count = 0
while count < len(l1):
    key[l1[count]] = m1[count]
    count+=1

print(key)

This returns (for example) :

{
  'A': 'G', 
  'C': 'A', 
  'B': 'Z', 
  'E': 'U', 
  'D': 'I', 
  'G': 'W', 
  'F': 'X', 
  'I': 'C', 
  'H': 'K', 
  'K': 'T', 
  'J': 'E', 
  'M': 'F', 
  'L': 'B', 
  'O': 'V', 
  'N': 'D', 
  'Q': 'M', 
  'P': 'L', 
  'S': 'S', 
  'R': 'J', 
  'U': 'Q', 
  'T': 'Y', 
  'W': 'H', 
  'V': 'R', 
  'Y': 'P', 
  'X': 'N', 
  'Z': 'O'
}
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
Sionae
  • 23
  • 6
  • 1
    Unrelated to the structure of your code, but please don't use lower-case L ("l") for a variable - especially right next to a 1 – Jeremy Weirich Jun 27 '16 at 18:07
  • 2
    Dicts don't track any ordering information. – user2357112 Jun 27 '16 at 18:07
  • 3
    Dictionaries are unordered. Read [this](http://stackoverflow.com/questions/15479928/why-is-the-order-in-python-dictionaries-and-sets-arbitrary) – idjaw Jun 27 '16 at 18:08
  • You can get all of the letters using the `string` library. For example, `m1 = [string.ascii_uppercase]` – Jeremy Weirich Jun 27 '16 at 18:13
  • I just don't get it why it seems to be in some kind of alphabetical order but not the exact one. Thank you for such a quick answer though. – Sionae Jun 27 '16 at 18:14
  • Python 3 uses hash randomization. Whatever order you *think* you are seeing in the result, it is a coincidence and not guaranteed to repeat the next time you run the code. The ordering is entirely arbitrary. – chepner Jun 27 '16 at 18:23

2 Answers2

1

As mentioned in the comments, dicts don't have an order. If you need for your dict to track order, consider using an collections.OrderedDict

viz:

import collections
key = collections.OrderedDict()
Him
  • 5,257
  • 3
  • 26
  • 83
1

You can do it using two string literals and zip:

m1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

l1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

key = {}

for m,l in zip(m1,l1):
    key.update({m:l})

print(key)

~

dmitryro
  • 3,463
  • 2
  • 20
  • 28