Advertisement
dougllio

wrdcnt.py

May 2nd, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # Simple program to get a word count of a phrase
  2. phrase = raw_input("Enter a phrase with no punctuation: ")
  3. print
  4.  
  5. # Use lower() and split() to create a list of lower case words
  6. phraseList = phrase.lower().split(" ")
  7. print "Created a lowercase list of words:", ", ".join(phraseList)
  8. print
  9.  
  10. # Create a set from that list. Sets eliminate duplicates, which helps us
  11. phraseSet = set(phraseList)
  12. print "Used the list to create a set of unique words:", ", ".join(phraseSet)
  13. print
  14.  
  15. # Use list comprehension to create a new list matrix (list of lists) each containing each word and its count.
  16. # The count comes from the phrase list, and the words come from the phrase set so we only get one of each word
  17. wordCounts = [[word,phraseList.count(word)] for word in phraseSet]
  18.  
  19. print "List comprehension created our unsorted word count using the list and set:"
  20. for each in wordCounts:
  21.   print each[1],"-",each[0]
  22. print
  23.  
  24. # Sort the word count by word. Straight forward sort
  25. wordCountsByWord = sorted(wordCounts)
  26.  
  27. print "Sorted By Word: "
  28. for each in wordCountsByWord:
  29.   print each[0],"-",each[1]
  30. print
  31.  
  32. # Sort the word count by count descending (reverse). In this case we need to tell sort to sort
  33. # using the second element of each sublist. We do this using the key parameter and lambda function
  34. wordCountsByCount = sorted(wordCounts,key=lambda x:x[1],reverse=True)
  35.  
  36. print "Sorted By Count: "
  37. for each in wordCountsByCount:
  38.   print each[1],"-",each[0]
  39. print
  40.  
  41. # We could also do everything in one shot - but it's not going to be as readable
  42. wordCountsOneShot = sorted([[word,phrase.lower().split(" ").count(word)] for word in set(phrase.lower().split(" "))],key=lambda x:x[1],reverse=True)
  43. print wordCountsOneShot
  44.  
  45.  
  46. # End
  47. phrase = raw_input("press ENTER to exit. ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement