Dictionary.py def load():混合变量

时间:2014-01-13 08:30:29

标签: python

好的,当在dict()中添加单词时,这些单词很有趣但是在再次加载之后,dict()项目都是混乱的。

   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

有没有办法让dict()项目相互混淆?

1 个答案:

答案 0 :(得分:3)

你应该使用OrderedDict,就像普通的词典一样使用它:

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

如您所见,它保留了广告订单。