-2

I need to pair the score of a movie review to every single word of the review line. For example, if the review line is "3 I love this movie". Then the dictionary should have:

dict = {3:I, 3:love, 3:this, 3:movie}

Here is my code:

#function to pair every single word with the score attributed to the review.

def pair_word_with_score(user_input):
    #create a dictionary
    pair_dict = {}
    words = user_input.split()
    for word in words:
        pair_dict[words[0]] = word
    return pair_dict



def main():
    user_input = input("Please enter a review \n")
#call the pairing function
    print(pair_word_with_score(user_input))
if __name__ == "__main__":
    main()

When I call the function, it only prints {3:movie}. The other values are overridden.

Prabhakar
  • 1,138
  • 2
  • 14
  • 30
  • 4
    Dictionaries must have unique keys. You can't have multiple `3` keys. From your title, are you trying to have a list of words stored against that key? The expected output in your question does not match what I understand from the question title. – roganjosh Dec 22 '17 at 20:36
  • you probably want to store the list of words with 3 as the key. – Jean-François Fabre Dec 22 '17 at 20:36
  • 3
    Or perhaps a dictionary of words pointing to the possible scores they represent? – Mad Physicist Dec 22 '17 at 20:38
  • Tell us what you want to do and we can help you out! – Anton vBR Dec 22 '17 at 21:58
  • Possible duplicate of [make dictionary with duplicate keys in python](https://stackoverflow.com/questions/10664856/make-dictionary-with-duplicate-keys-in-python) – ndrwnaguib Dec 22 '17 at 21:58

2 Answers2

1

Dictionaries can only have unique keys, which means you can't create:

3 : 'I'
3 : 'love'
...

In this case 3 is the key and 'I' is a value.

I guess that you want to have relation:

number -> list_of_words

Here is how you can create dictionary from following sentence where: 3 is a key and words are the value:

>>> text = "3 I love this movie"
>>> my_dict = dict(zip([int(text[0])], [text[1:].split()]))
>>> my_dict
{3: ['I', 'love', 'this', 'movie']}

dict stands for constructor of a dictionary, in this case it cast tuple to key,value pair.

zip combines two iterable objects (in this case lists) into tuples.

If you want to have key as a number you need to cast the first character into type int()

[1:] means that I'm reading all characters except the first one - it returns list without item at index 0

split() function separates words in a string by separator (in this case whitespace which is default separator). It returns list of words.

Now you can call:

>>> my_dict[3]

and you get

['I', 'love', 'this', 'movie']

You can make it in this way (more readable):

>>> my_dict = {}
>>> my_dict[int(text[0])] = text[1:].split()
>>> my_dict
{3: ['I', 'love', 'this', 'movie']}

Don't use "dict" for variable name cause you shadow name "dict()" which stand for dictionary constructor.

Persi
  • 414
  • 6
  • 15
0
def pair_word_with_score(user_input):
    #create a dictionary
    pair_dict = {}
    words = user_input.split()
    for word in words:
        pair_dict[word] = words[0]
    return pair_dict



def main():
    user_input = input("Please enter a review \n")
#call the pairing function
    print(pair_word_with_score(user_input))
if __name__ == "__main__":
    main()

now you can get I:3 love:3 and for other if you have same word like " 4 i love it" you try to write code like adding to 3 and creating a list [3,4] so you can compute for how many reviews love is there like that