Python 从文本文件中除以行中的每个整数

Python divide each integer in line from text file

我有以下代码:

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
.
.
.

等等

使用 count = lines[1] 而不是 count = lines([1]) 来访问列表的元素。您还需要使用float()来转换它。

试试这个:

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)