嘿伙计们,我已经设置了两个字典,它们具有相同的键但具有不同的值。我试图让代码像这样打印出来
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.
Okay so the Data File looks like this (only doing some numbers because the file is pretty big
Census Data Alabama Winfield 4534 Alabama Woodland 208 Alabama Woodstock 1081 Alabama Woodville 743 Alabama Yellow Bluff 175 Alabama York 2477 Alaska Adak 361
the count is the number of occurences of the first digit of the number. I basically turned each line into a list and appended the last value of the list (the number) to a new list. So then I did a Count for how many times 1, 2, 3, 4, 5, 6 , 7, 8 ,9 appear. That's what countList represents. So I stored that in a dictionary with the keys being the digits and the counts being the values. The % is the relative frequency of the count. So I set up a new list and calculated the relative frequency which is basically the count + the sum of all the counts and rounded it off to one digit. The % column has the relative count of each digit. I put that into a dictionary also where the keys are the digits 1, 2, 3, 4, 5, 6, 7, 8, 9. So now I just need to print these numbers into the 3 columns,
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()
最佳答案
使用您的代码,您只需要做:
for i in range(1, 10):
print (i, dictCount[i], freqCount[i])
但是你可以简化它很多:
import collections
data = []
with open("Census__2008.txt") as fh:
fh.readline() # skip first line
for line in fh:
value = line.split()[-1]
data.append(value)
c = collections.Counter([x[0] for x in data])
total = sum(c.values())
print("Digit", "Count", "%")
for k, v in sorted(c.iteritems()):
freq = v / total * 100
round_freq = round(freq, 1)
print(k, v, round_freq)
关于python - 如何按 3 列打印 2 本词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18243513/