我有一个元组列表
[('By', 'IN'), ('now', 'RB'), (',', ','), ('Cain', 'NNP'), ('has', 'VBZ'), ('created', 'VBN'), ('a', 'DT'), ('small', 'JJ'), ('amount', 'NN'), ('of', 'IN'), ('sympathy', 'NN'), ('for', 'IN'), ('introverts', 'NNS'), ('through', 'IN'), ('the', 'DT'), ('pathos', 'NNS'), ('described', 'VBD'), ('earlier.', 'NNP'), ('After', 'NNP'), ('all', 'DT'), ('of', 'IN'), ('the', 'DT'), ('groundwork', 'NN'), ('is', 'VBZ'), ('laid', 'VBN'), ('down', 'RP'), (',', ','), ('she', 'PRP'), ('begins', 'VBZ'), ('to', 'TO'), ('detail', 'VB'), ('her', 'PRP$'), ('research', 'NN'), ('which', 'WDT'), ('takes', 'VBZ'), ('a', 'DT'), ('more', 'RBR'), ('serious', 'JJ'), ('tone', 'NN'), (',', ','), ('rather', 'RB'), ('than', 'IN'), ('the', 'DT'), ('humorous', 'JJ'), ('anecdotes.', 'NNP'), ('She', 'NNP'), ('transitions', 'NNS'), ('fairly', 'RB'), ('well', 'RB'), ('from', 'IN'), ('her', 'PRP$'), ('own', 'JJ'), ('personal', 'JJ'), ('examples', 'NNS'), ('right', 'RB'), ('into', 'IN'), ('her', 'PRP$'), ('research', 'NN'), ('.', '.')]
当我使用dict()将该列表转换为dict时,顺序会发生变化吗?这是为什么?我该怎么做才能使订单不变? 这是转换后的字典
{'all': 'DT', 'own': 'JJ', 'introverts': 'NNS', 'fairly': 'RB', 'groundwork': 'NN', 'is': 'VBZ', 'detail': 'VB', 'down': 'RP', 'sympathy': 'NN', 'right': 'RB', 'through': 'IN', 'examples': 'NNS', 'tone': 'NN', 'transitions': 'NNS', 'described': 'VBD', 'takes': 'VBZ', 'for': 'IN', 'rather': 'RB', 'humorous': 'JJ', 'After': 'NNP', ',': ',', 'research': 'NN', 'to': 'TO', 'laid': 'VBN', 'which': 'WDT', 'has': 'VBZ', 'By': 'IN', 'more': 'RBR', 'begins': 'VBZ', 'earlier.': 'NNP', 'than': 'IN', 'anecdotes.': 'NNP', 'from': 'IN', 'She': 'NNP', 'Cain': 'NNP', 'now': 'RB', 'pathos': 'NNS', 'a': 'DT', 'her': 'PRP$', 'created': 'VBN', 'of': 'IN', 'into': 'IN', 'well': 'RB', 'personal': 'JJ', 'amount': 'NN', 'she': 'PRP', '.': '.', 'small': 'JJ', 'serious': 'JJ', 'the': 'DT'}
这是我的简单python脚本
import nltk
text = nltk.word_tokenize("By now, Cain has created a small amount of sympathy for introverts through the pathos described earlier. After all of the groundwork is laid down, she begins to detail her research which takes a more serious tone, rather than the humorous anecdotes. She transitions fairly well from her own personal examples right into her research. ")
tagged =nltk.pos_tag(text)
mydict = dict(tagged)
print(tagged)
print('\n')
print(mydict)
提前致谢
答案 0 :(得分:5)
答案 1 :(得分:0)
在Python 2.7中,您可以使用Ordered Dict http://docs.python.org/dev/library/collections.html#collections.OrderedDict