brewersfan1976

ch3-ex5.py

Jul 15th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. # program that breaks a string into characters and stores them in dictionary
  2. # also stores the number of each characters in a string and displays them in descending order
  3.  
  4. from collections import Counter
  5.  
  6. # get input from user
  7.  
  8. userInput = raw_input("Please enter a sentence: ")
  9.  
  10. # break the string into separate characters and store them in a list
  11.  
  12. characters = list(userInput)
  13.  
  14. # count the number of each character in the string
  15.  
  16. character_count = Counter(characters)
  17.  
  18. # store the number of characters in descending order
  19.  
  20. character_count_descending = character_count.most_common()
  21.  
  22. # create a dictionary
  23.  
  24. d = {}
  25.  
  26. # store the list in a dictionary
  27.  
  28. d = dict(character_count)
  29.  
  30. print d
  31.  
  32. # print sorted dictionary
  33.  
  34. print character_count_descending
Advertisement
Add Comment
Please, Sign In to add comment