Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. def digit_count (leading_digits, dig_occurrence):
  2.  
  3. for i in leading_digits:
  4. if i in dig_occurrence:
  5. dig_occurrence[i] += 1
  6. return dig_occurrence
  7.  
  8. def main():
  9. pop_num = []
  10. inFile = open ("./Census_2009.txt", "r")
  11. count = 0
  12. for line in inFile:
  13. if (count == 0):
  14. count += 1
  15. continue
  16. else:
  17. count += 1
  18. line = line.strip()
  19. word_list = line.split()
  20. pop_num.append (word_list[-1])
  21.  
  22. # get_freq(word_list)
  23.  
  24. inFile.close()
  25. leading_digits = []
  26. for i in range (len(pop_num)):
  27. temp = pop_num[i]
  28. leading_digits.append(temp[0])
  29.  
  30.  
  31. print (pop_num)
  32.  
  33. # Create a dictionary
  34. dig_occurrence = {'1':0 , '2':0, '3':0, '4':0, '5':0, '6':0, '7':0, '8':0, '9':0 }
  35.  
  36. # Get frequency distribution
  37. dig_occurrence = digit_count(leading_digits, dig_occurrence)
  38.  
  39.  
  40. # Print output table
  41. print ("Digit\tCount\t%")
  42. total_digits = len(leading_digits)
  43. for i in range (1, 10):
  44. # Calculate percentage of frequency distribution
  45. frequency = 100 * (dig_occurrence[str(i)] / total_digits)
  46. temp ="{:<8}{:<8}{}".format(str(i), dig_occurrence, frequency)
  47. print (temp)
  48. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement