How to Print 2 dictionaries in 3 columns
Hey guys so I have set up 2 dictionaries which have the same keys but
different values for both. I am trying to get the code to print out like
this
Digit Count %
1
2
3
4
5
6
7
8
9
The count is the countList and the % is the numFreq Values with their
numbers also going down in the Count and % respectively.
Here is my code so far
def main():
num_freq = {}
pop_num = []
inFile = open ("Census__2008.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])
counts = {}
for x in pop_num:
k = str(x)[0]
counts.setdefault(k, 0)
counts[k] += 1
countList = [counts[str(i)] for i in range(1,10)]
sumList = sum(countList)
dictCount = {}
dictCount[1] = countList[0]
dictCount[2] = countList[1]
dictCount[3] = countList[2]
dictCount[4] = countList[3]
dictCount[5] = countList[4]
dictCount[6] = countList[5]
dictCount[7] = countList[6]
dictCount[8] = countList[7]
dictCount[9] = countList[8]
num_Freq = []
for elm in countList:
rel_Freq = 0
rel_Freq = rel_Freq + ((elm / sumList) * 100.0)
rel_Freq = round(rel_Freq, 1)
num_Freq.append(rel_Freq)
freqCount = {}
freqCount[1] = num_Freq[0]
freqCount[2] = num_Freq[1]
freqCount[3] = num_Freq[2]
freqCount[4] = num_Freq[3]
freqCount[5] = num_Freq[4]
freqCount[6] = num_Freq[5]
freqCount[7] = num_Freq[6]
freqCount[8] = num_Freq[7]
freqCount[9] = num_Freq[8]
print ("Digit" " ", "Count", " ", "%")
print (
main()
No comments:
Post a Comment