Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - sentence to a dictionary

Tags:

python

I am trying to write a code, which takes a sentence:

dimension implies direction implies measurement implies the more and the less

and converts it into a dictionary, where the words = key and the value = previous words, but for the first word there is NO value.

It should essentially be:

{'and' : 'more'

'dimension' : ''

'direction' : 'implies'

'implies' : 'dimension', 'direction', 'measurement'

'less' : 'the'

'measurement' :'implies'

'more' : 'the'

'the' : 'and', 'implies'}

I wrote:

def get_previous_words_dict(text):
    words_list = text.split()
    sentence_dict = {}
    for i in range(0,len(words_list)):
        sentence_dict[words_list[i]] = words_list[i-1]

BUT it doesn't add the value to the existing value of a key, but rather replaces it, so instead of getting 3 different values for 'implies' I am only getting 1 value.

Also, instead of assigning NO value to the word dimension, it is assigning it less (since -1).

like image 344
Nume Avatar asked Feb 05 '16 06:02

Nume


2 Answers

Here's how to do it without a defaultdict:

text = 'dimension implies direction implies measurement implies the more and the less'
sentence_dict = {}
prev = ''
for word in text.split():
    if word not in sentence_dict:
        sentence_dict[word] = []
    sentence_dict[word].append(prev)
    prev = word

print(sentence_dict)

output

{'and': ['more'], 'direction': ['implies'], 'implies': ['dimension', 'direction', 'measurement'], 'less': ['the'], 'measurement': ['implies'], 'the': ['implies', 'and'], 'dimension': [''], 'more': ['the']}

Here's a more compact way, using setdefault:

text = 'dimension implies direction implies measurement implies the more and the less'

sentence_dict = {}
prev = ''
for word in text.split():    
    sentence_dict.setdefault(word, []).append(prev)
    prev = word

print(sentence_dict)

The previous version is probably a little easier to read.

like image 160
PM 2Ring Avatar answered Nov 19 '22 02:11

PM 2Ring


Just split the string in to a list and create another list by offsetting with a prefix empty string, then zip it and create the dictionary by iterating it, PS - use defaultdict initialized with list instead of dictionary because of the possiblity of multiple values for a single key.

inp = "dimension implies direction implies measurement implies the more and the less"
l1 = inp.split()
l2 = [""]+l1;
zipped = zip(l1,l2)
from collections import defaultdict
d = defaultdict(list)
for k, v in zipped: 
    d[k].append(v)
print d

If you don't want to import any thing initialize the dict to consist of empty list then use the same logic

inp = "dimension implies direction implies measurement implies the more and the less"
l1 = inp.split()
l2 = [""]+l1;
zipped = zip(l1, l2)
d = {x: [] for x in l1}
for k, v in zipped: 
    d[k].append(v)
print d
like image 40
Kavin Eswaramoorthy Avatar answered Nov 19 '22 03:11

Kavin Eswaramoorthy