Reputation: 107
Okay when putting words in the dict() the words are fun but after they are loaded again the dict() items are all jumbled around.
def load():
dWord = {}
for word in open("Dictionary.DB","r").readlines():
if len(word.strip())>0:
raw = word.split()
word, name, numdef, definition, wordtime = ' '.join(raw[0:-4]), raw[1], raw[2], ' '.join(raw[3:-1]), raw[-1]
dWord[word] = name, int(numdef), definition, int(wordtime)
Dictionary.dWord = dWord
Is there a way to keep the dict() items from mixing up with each other?
Upvotes: 0
Views: 81
Reputation: 2339
You should use OrderedDict, use it just like normal dict:
>>> from collections import OrderedDict
>>> d = OrderedDict()
>>> d[2] = 3
>>> d[1] = 4
>>> d[5] = 2
>>> print d
OrderedDict([(2, 3), (1, 4), (5, 2)])
>>> for i in d:
>>> print i
2
1
5
As you see it keeps the insertion order.
Upvotes: 3