Reputation: 1049
Ive got a list of tuples
[('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'), ('.', '.')]
And when i convert that list to a dict using dict(), the order changes? Why is that? What can I do to make the order not change? Here is the converted 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'}
Here is my simple python script
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)
Thanks in advance
Upvotes: 0
Views: 202
Reputation: 383
In Python 2.7 you can use Ordered Dict http://docs.python.org/dev/library/collections.html#collections.OrderedDict
Upvotes: 0
Reputation: 310187
From the documentation on dictionaries:
It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).
Use a collections.OrderedDict
instead.
Upvotes: 5