I have following string:
>>>sentence='No, I shouldn't be glad, YOU should be glad.'
And what I want is to make a dictionary with a word of the sentence as key, and the next word as value.
>>>dict(sentence)
{('No,'): ['I'], ('I'): ['shouldn't'], ('shouldn't'): ['be'], ('be'): ['glad,', 'glad.'], ('glad,'): ['YOU'], ('YOU'): ['should'], ('should'): ['be']}
^ ^ ^
| | |
As you can see if a word occurs multiples times in a sentence, it gets multiple values. If it’s the last word it will not be added to the dictionary. ‘glad’ doesn’t get multiple values because the word ends with a ‘,’ or ‘.’ which makes it a different string.
This produces