0

タプルのリストを取得しました

[('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 に変換すると、順序が変わりますか? 何故ですか?順序が変わらないようにするにはどうすればよいですか? ここに変換された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)

前もって感謝します

4

2 に答える 2

5

辞書に関するドキュメントから:

ディクショナリは、キーが (1 つのディクショナリ内で) 一意であるという要件を備えた、キーと値のペアの順序付けられていないセットと考えるのが最善です。

collections.OrderedDict代わりにaを使用してください。

于 2013-10-07T21:16:17.023 に答える
0

Python 2.7 では、Ordered Dict http://docs.python.org/dev/library/collections.html#collections.OrderedDictを使用できます。

于 2013-10-07T21:20:57.863 に答える