Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Simple program to get a word count of a phrase
- phrase = raw_input("Enter a phrase with no punctuation: ")
- print
- # Use lower() and split() to create a list of lower case words
- phraseList = phrase.lower().split(" ")
- print "Created a lowercase list of words:", ", ".join(phraseList)
- print
- # Create a set from that list. Sets eliminate duplicates, which helps us
- phraseSet = set(phraseList)
- print "Used the list to create a set of unique words:", ", ".join(phraseSet)
- print
- # Use list comprehension to create a new list matrix (list of lists) each containing each word and its count.
- # The count comes from the phrase list, and the words come from the phrase set so we only get one of each word
- wordCounts = [[word,phraseList.count(word)] for word in phraseSet]
- print "List comprehension created our unsorted word count using the list and set:"
- for each in wordCounts:
- print each[1],"-",each[0]
- print
- # Sort the word count by word. Straight forward sort
- wordCountsByWord = sorted(wordCounts)
- print "Sorted By Word: "
- for each in wordCountsByWord:
- print each[0],"-",each[1]
- print
- # Sort the word count by count descending (reverse). In this case we need to tell sort to sort
- # using the second element of each sublist. We do this using the key parameter and lambda function
- wordCountsByCount = sorted(wordCounts,key=lambda x:x[1],reverse=True)
- print "Sorted By Count: "
- for each in wordCountsByCount:
- print each[1],"-",each[0]
- print
- # We could also do everything in one shot - but it's not going to be as readable
- wordCountsOneShot = sorted([[word,phrase.lower().split(" ").count(word)] for word in set(phrase.lower().split(" "))],key=lambda x:x[1],reverse=True)
- print wordCountsOneShot
- # End
- phrase = raw_input("press ENTER to exit. ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement