Change a sentence in a dictionary in Python

I have the following line:

>>>sentence='No, I shouldn't be glad, YOU should be glad.' 

And I want to make a dictionary with a sentence word as a key, and the next word as a 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 multiple times in a sentence, it receives several meanings. If this is the last word, it will not be added to the dictionary. "Glad" does not receive multiple meanings, because the word ends with the symbol "," or ".". making it another line.

+4
source share
5 answers
 import collections sentence = "No, I shouldn't be glad, YOU should be glad." d = collections.defaultdict(list) words = sentence.split() for k, v in zip(words[:-1], words[1:]): d[k].append(v) print(d) 

It creates

 defaultdict(<type 'list'>, {'No,': ['I'], 'be': ['glad,', 'glad.'], 'glad,': ['YOU'], 'I': ["shouldn't"], 'should': ['be'], "shouldn't": ['be'], 'YOU': ['should']}) 
+4
source

Using dict.setdefault() :

 In [9]: strs = "No, I shouldn't be glad, YOU should be glad." In [19]: dic = {} In [20]: for x, y in zip(words, words[1:]): dic.setdefault(x, []).append(y) ....: In [21]: dic Out[21]: {'I': ["shouldn't"], 'No,': ['I'], 'YOU': ['should'], 'be': ['glad,', 'glad.'], 'glad,': ['YOU'], 'should': ['be'], "shouldn't": ['be']} 
+3
source

This is not verified, but should be close.

 words = sentence.split() sentenceDict = {} for index in xrange(len(words)-1): if words[index] in sentenceDict: sentenceDict[words[index].append(words[index+1]) else sentenceDict[words[index]] = [words[index+1]] 
0
source

If order is not important, just another way to do it

 sentence="No, I shouldn't be glad, YOU should be glad." #Split the string into words sentence = sentence.split() #Create pairs of consecutive words sentence = zip(sentence,sentence[1:]) from itertools import groupby from operator import itemgetter #group the sorted pairs based on the key sentence = groupby(sorted(sentence, key = itemgetter(0)), key = itemgetter(0)) #finally create a dictionary of the groups {k:[v for _,v in g] for k, g in sentence} {'No,': ['I'], 'be': ['glad,', 'glad.'], 'glad,': ['YOU'], 'I': ["shouldn't"], 'should': ['be'], "shouldn't": ['be'], 'YOU': ['should']} 
0
source
 import collections sentence = "No, I shouldn't be glad, YOU should be glad." d = collections.defaultdict(list) words = sentence.split() for k, v in zip(words[:-1], words[1:]): d[k].append(v) print(d) 

It creates

 defaultdict(<type 'list'>, {'No,': ['I'], 'be': ['glad,', 'glad.'], 'glad,': ['YOU'], 'I': ["shouldn't"], 'should': ['be'], "shouldn't": ['be'], 'YOU': ['should']}) 

@NLS: I just wanted to add something. "d = collections.defaultdict (list)", like the dict object, does not preserve the word order, so if we need to preserve the order of the sentence, we may have to use a tuple.

0
source

All Articles