-1

I'm trying to put file's contents in a dictionary. Even though file itself remains unchanged, the dictionary I get in the end is always different.

with open('sorted.txt') as fin:
    lines = fin.readlines()
    words = {i.split(' - ')[0]:i.split(' - ')[1] for i in lines}

    for i in words.keys():
        print(i, '-', words[i])

One time it's something like this:

bawl - реветь, вопить
by the by - кстати, к слову (cм. by the way)
staple - cкоба, основная черта, главная часть
staggering - ошеломляющий, неустойчивый
trample - давить, топтать, подавлять
enthrall - очаровывать, увлекать, захватывать
elusive - неуловимый
lay off - увольнять
at the drop of a hat - Fig. immediately; instantly; on the slightest signal or urging.
feasible - осуществимый, выполнимый
wince - вздрогнуть, поморщиться
wax poetic - Fig. to speak poetically. I hope you will pardon me if I wax poetic for a moment when I say that your lovely hands drift across the piano keys like swans on a lake.
supple - мягкий, податливый
willy-nilly - spontaneous
itsy-bitsy - незначительный
ramble - прогуливаться для удовольствия, бродить без цели
get off on sth - тащиться по чему-л.
bring out - выявлять, обнаруживать
apples and oranges - сравнение несравнимых вещей или понятий
backlash - агрессивная реакция
eat sb out of house and home - есть чужую еду
meek - кроткий, смиренный
chuckle - посмеиваться, хихикать
gibberish - неразборчивая речь, чушь
evocative of - вызывающий воспоминания о чем-л.
supplication - мольба
fall apart (inf.) - расчувствоваться, испытывать эмоциональные проблемы
fumble - шарить, нащупывать
on an ad-hoc basis - as demand arises, по необходимости
plod along
stammer - запинаться
exasperate - раздражать, изводить, выводить из себя
conspicuous - бросающийся в глаза, заметный
to walk on eggshells - прилагать большие усилия, чтобы не расстроить кого-л.
go out with a bang - If someone or something goes out with a bang, they stop 
existing or doing something in an exciting way.

And another exact same list but shuffled.

What am I doing wrong?

SiHa
  • 5,520
  • 12
  • 23
  • 37
  • 5
    Possible duplicate of [Python dictionary, how to keep keys/values in same order as declared?](https://stackoverflow.com/questions/1867861/python-dictionary-how-to-keep-keys-values-in-same-order-as-declared) – Chris_Rands Jul 20 '17 at 10:07

2 Answers2

0

It's not that it's reshuffling - it's that the order is non-deterministic to begin with.

Why is dictionary ordering non-deterministic?

There is a subclass to handle this - OrderedDict in collections.

j4nw
  • 1,720
  • 9
  • 23
-2

Have a look a the Python manual:

https://docs.python.org/3.5/tutorial/datastructures.html

5.4. Sets

Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

You may consider sorting dictionary keys.

gbajson
  • 1,201
  • 9
  • 22