嘿伙计们,所以我设置了两个词典,它们具有相同的键但两者的值都不同。我试图让代码像这样打印出来
Digit Count % 1 2 3 4 5 6 7 8 9
count是countList,%是numFreq值,它们的数字也分别在Count和%中下降。
好的,所以数据文件看起来像这样(只做一些数字,因为文件非常大
Census Data Alabama Winfield 4534 Alabama Woodland 208 Alabama Woodstock 1081 Alabama Woodville 743 Alabama Yellow Bluff 175 Alabama York 2477 Alaska Adak 361
计数是该数字的第一个数字的出现次数。我基本上将每一行转换为一个列表,并将列表的最后一个值(数字)附加到一个新列表中。那么我做了一次计数,看出1,2,3,4,5,6,7,8,9出现多少次。这就是countList所代表的。所以我将它存储在字典中,其中键是数字,计数是值。 %是计数的相对频率。所以我设置了一个新的列表并计算了相对频率,它基本上是计数+所有计数的总和,并将其四舍五入为一位数。 %列具有每个数字的相对计数。我把它放入字典中,其中键是数字1,2,3,4,5,6,7,8,9。所以现在我只需要将这些数字打印到3列中,
到目前为止,这是我的代码
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()
答案 0 :(得分:1)
使用您的代码,您只需要:
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)