我有以下代码:
total = 49971
with open("user1_wordcount.txt",'r') as f:
for line in f:
lines = line.split()
count = lines([1])
mean = count/total
print(mean)
文本文件由以下格式的行组成:
Google 348
Amazon 120
.
.
.
所以我需要将每个数字除以49971,输出结果类似于:
Google 0.00696404
.
.
.
等
答案 0 :(得分:1)
使用count = lines[1]
代替count = lines([1])
来访问列表元素。您还需要使用float()
进行转换。
答案 1 :(得分:1)
尝试一下:
total = 49971
with open("user1_wordcount.txt",'r') as f:
for line in f:
company_name, count = line.rstrip().split()
print( company_name, float(count)/total)