Advertisement
Guest User

python-chart-basic

a guest
Aug 9th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Aug 05 12:53:57 2015
  4.  
  5. reading survey #openintro
  6.  
  7. @author: Sabi
  8. """
  9. # Create an empty dictionary for associating radish names
  10. # with vote counts
  11. from pprint import pprint
  12. import matplotlib.pyplot as plt
  13. counts = {}
  14. voted = []
  15. for line in open("radishsurvey.txt"):
  16.    
  17.     line1 = line.strip()
  18.     #print line
  19.    
  20.     name, vote = line1.split(" - ")
  21.     vote = vote.strip().capitalize()
  22.     vote = vote.replace("  "," ")
  23.     if name in voted:
  24.         print name, "has already voted"
  25.         continue
  26.     voted.append(name)
  27.     if vote not in counts:
  28.         # First vote for this variety
  29.         counts[vote] = 1
  30.     else:
  31.         # Increment the vote count
  32.         counts[vote] = counts[vote] + 1
  33. """    
  34. for name in counts:
  35.    print name, counts[name]      
  36. """
  37. def find_winner(counts):
  38.     winner = ""
  39.     pre_vote = 0
  40.     for vote in counts:
  41.         if counts[vote] >= pre_vote:
  42.            
  43.             winner = vote
  44.             pre_vote = counts[vote]
  45.            
  46.            
  47.     return winner, pre_vote
  48.    
  49. def find_loser(counts):
  50.     loser, pre_vote = find_winner(counts) #calling a function inside another fn.
  51.     for vote in counts:
  52.         if counts[vote] < pre_vote:
  53.            
  54.             loser = vote
  55.             pre_vote = counts[vote]
  56.            
  57.     return loser, pre_vote
  58.  
  59. #pprint(counts)
  60.                  
  61. for item in counts:
  62.     print item, counts[item]
  63. winner,votes = find_winner(counts)
  64. print "And the winner is Mr. %s with %d votes" %(winner,votes)
  65. loser,votes = find_loser(counts)
  66. print "Sorry, the loser is Mr. %s with %d votes" %(loser,votes)
  67.  
  68. plt.bar(range(len(counts)), counts.values(), align='center')
  69. plt.ylabel(s = "Votes")
  70. plt.xticks(range(len(counts)), counts.keys(),rotation=90)
  71. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement