Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- for line in file:
- print line
- """
- def word_map(string):
- word_dict = {}
- for word in string.split():
- word = filter(str.isalnum, word).lower()
- word = word.strip()
- if word != '':
- if word in word_dict.keys():
- word_dict[word] +=1
- else:
- word_dict[word] = 1
- return word_dict
- """
- my_dict= word_map("This is a string , this is another string too")
- for k in my_dict:
- print k, my_dict[k]
- """
- file = open("story.txt", "r")
- file_string = file.read()
- words = word_map(file_string)
- popular_words = sorted(words, key=words.get, reverse=True)
- for k in popular_words:
- print k, popular_words[k]
- /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/mona/PycharmProjects/PythonCodes/top_words.py
- the
- Traceback (most recent call last):
- File "/Users/mona/PycharmProjects/PythonCodes/top_words.py", line 27, in <module>
- print k, popular_words[k]
- TypeError: list indices must be integers, not str
- Process finished with exit code 1
Add Comment
Please, Sign In to add comment