string = 'Hello? My name is Jane Bond, I am a MI6 Agent '#Beware of this last white space, it prevents Agent from joining the next Hello? (AgentHello?) string += 'Hello? My name is James Bond, I am a MI6 Agent' wordlist = string.split() frequency = [] for w in wordlist: frequency.append(wordlist.count(w)) print("String\n" + string +"\n") print("List\n" + str(wordlist) + "\n") print("Frequencies\n" + str(frequency) + "\n") print("Pairs\n" + str(zip(wordlist, frequency))) """ Line 1, we create a variable and assign a string to it. Line 2, += tells the program to append something to the end of an existing variable. Line 4, we split the string into a list. Line 6, We then create an (initially empty) list called frequency, go through each word in the wordlist, and count the number of times that word appears in the whole list. We then add each word’s count to our frequency list Using the zip operation, we are able to match the first word of the word list with the first number in the frequency list, the second word and second frequency, and so on. We end up with a list of word and frequency pairs. The str function converts any object to a string so that it can be printed. """
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question