hamid
hamid

Reputation: 9

Build a dictionary with the words of a sentence as keys and the number of position of the words from 1 as values in python

I expect this output from the code below:

{'Tell': 1, 'a': 2, 'little': 3, 'more': 4, 'about': 5, 'yourself': 6, 'as': 7, 'a': 8, 'developer': 9}

But I get this output:

{'Tell': 1, 'a': 8, 'little': 3, 'more': 4, 'about': 5, 'yourself': 6, 'as': 7, 'developer': 9}

This is the code:

sentence = 'Tell a little more about yourself as a developer'
list_words = sentence.split()

d = {word: i for i, word in enumerate(list_words, 1)}

print(d)

What do you think is the problem? What is the code that gives the output I want?

Upvotes: -1

Views: 1198

Answers (4)

Ahmed
Ahmed

Reputation: 53

Maybe you printed the index of the letters instead of the index of the words.

You can try:

sentence = 'Tell a little more about yourself as a developer'

words_list = sentence.split()

words_dictionary = dict()

for word in words_list:
    words_dictionary[word] = words_list.index(word) + 1

print(words_dictionary)

#output :
# {'Tell': 1, 'a': 2, 'little': 3, 'more': 4, 'about': 5, 'yourself': 6, 'as': 7, 'developer': 9}

Upvotes: 0

Life is complex
Life is complex

Reputation: 15619

You need to access the Index of the list to get the order of the words in your sentence.

sentence = 'Tell a little more about yourself as a developer'
list_words = sentence.split()

words = [(value, index+1) for index, value in enumerate(list_words)]
print(words)

#output 
[('Tell', 1), ('a', 2), ('little', 3), ('more', 4), ('about', 5), ('yourself', 6), ('as', 7), ('a', 8), ('developer', 9)]

Your requested output is a dictionary, but in a specific order. Python dictionaries don't support duplicate keys (a, a), which creates problems with getting this output.

sentence = 'Tell a little more about yourself as a developer'
list_words = sentence.split()

words = [(value, index+1) for index, value in enumerate(list_words)]

dict_words = {}
for item in words:
    dict_words.update({item[0]:item[1]})

print(dict_words)

#output
{1: 'Tell', 2: 'a', 3: 'little', 4: 'more', 5: 'about', 6: 'yourself', 7: 'as', 8: 'a', 9: 'developer'}

Upvotes: 1

Alain T.
Alain T.

Reputation: 42133

You cannot have two identical keys in a dictionary so it is impossible to get your expected result where 'a' is present twice (once for 'a':2 and again for 'a':8).

You output data structure could be a list of tuples instead of a dictionary:

r = [(word,i) for i,word in enumerate(list_words,1)]

[('Tell', 1), ('a', 2), ('little', 3), ('more', 4), ('about', 5), 
 ('yourself', 6), ('as', 7), ('a', 8), ('developer', 9)]

Or, it could be a dictionary with a list of positions for each word:

d = dict()
for i,word in enumerate(list_words,1):
    d.setdefault(word,[]).append(i)

{'Tell': [1], 'a': [2, 8], 'little': [3], 'more': [4], 
 'about': [5], 'yourself': [6], 'as': [7], 'developer': [9]}

Upvotes: 2

Shantanu
Shantanu

Reputation: 11

sentence ='Tell a little more about yourself as a developer'
list_words=sentence.split()
uniquewords = list(set(list_words))
d = {i:0 for i in uniquewords}
for i in list_words:
    for j in s1:
        if i==j:
            d[j]+=1
print(d)

Upvotes: 0

Related Questions