Here is the question:
Write a function named wordPositions() with the following input and output. Input: s, a string consisting of upper and lower case letters and spaces. Return: a dictionary in which each distinct word in s is a key and the corresponding value is a list of the positions in s in which the word occurs. Words are to be treated as the same regardless of their capitalization. That is, "Yes" and "yes" are the same word. The following is an example of correct output.
s = 'One fish two fish red fish blue fish'
wp = wordPositions(s)
print(wp)
{'two': [2], 'one': [0], 'red': [4], 'fish': [1, 3, 5, 7], 'blue': [6]}
Now here is my code:
def wordPositions(s):
aDict = {}
words = s.split(' ')
for item in words:
position = words.index(item)
aDict[item] = position
print(aDict)
print(wordPositions('One fish two fish red fish blue fish'))
The issue is my output:
{'two': 2, 'blue': 6, 'red': 4, 'fish': 1, 'One': 0}
How do I get it to look like the professor's? Also, notice how in my output, the word 'fish' shows only one position of it although it is repeated in the string. How do I get Python to show the multiple positions of 'fish'?