[英]Python How to calculate percentage of a text found in every row
我有一个包含一列和 4000 行的 CVS 我想制作一个脚本来打印每个唯一的单词及其在该 CSV 上的百分比
例子:
Trojan
Trojan
redirects
Exploits
Trojan
木马:60% 重定向:20% 漏洞利用 20%
什么是简单/简单的方法来做到这一点?
import csv
myDict = {}
with open('export.csv', 'rb') as csvfile:
for word in csvfile:
if word in myDict:
myDict[word] += 1
else:
myDict[word] = 1
for word in myDict:
print word, float(myDict[word])/len(csvfile)
您可以使用 set 获取所有唯一值并使用 count 获取出现次数。 用文本除以列表的长度得出百分比:
text = ['a', 'a', 'b', 'c']
[(i, text.count(i) * 100. / len(text)) for i in set(text)]
导致:
[('a', 50.0), ('b', 25.0), ('c', 25.0)]
您可以使用字典如下:
import csv
myDict = {}
row_number = 0
with open('some.csv', 'rb') as f:
reader = csv.reader(f, delimiter=' ')
for row in reader:
row_number +=1
if row[0] in myDict:
myDict[row[0]] += 1
else:
myDict[row[0]] = 1
for word in myDict:
print word, float(myDict[word])/row_number
工作原理如下:
>>> ================================ RESTART ================================
>>>
Trojan 0.6
Exploits 0.2
redirects 0.2
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.