Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def digit_count (leading_digits, dig_occurrence):
- for i in leading_digits:
- if i in dig_occurrence:
- dig_occurrence[i] += 1
- return dig_occurrence
- def main():
- pop_num = []
- inFile = open ("./Census_2009.txt", "r")
- count = 0
- for line in inFile:
- if (count == 0):
- count += 1
- continue
- else:
- count += 1
- line = line.strip()
- word_list = line.split()
- pop_num.append (word_list[-1])
- # get_freq(word_list)
- inFile.close()
- leading_digits = []
- for i in range (len(pop_num)):
- temp = pop_num[i]
- leading_digits.append(temp[0])
- print (pop_num)
- # Create a dictionary
- dig_occurrence = {'1':0 , '2':0, '3':0, '4':0, '5':0, '6':0, '7':0, '8':0, '9':0 }
- # Get frequency distribution
- dig_occurrence = digit_count(leading_digits, dig_occurrence)
- # Print output table
- print ("Digit\tCount\t%")
- total_digits = len(leading_digits)
- for i in range (1, 10):
- # Calculate percentage of frequency distribution
- frequency = 100 * (dig_occurrence[str(i)] / total_digits)
- temp ="{:<8}{:<8}{}".format(str(i), dig_occurrence, frequency)
- print (temp)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement