Python查找CSV中出现最多的单词

2024-07-03 07:58:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在CSV行中找到最常出现的子字符串,或者单独查找,或者使用关键字列表查找。你知道吗

我已经找到了一种方法,使用Python通过下面的响应找出CSV文件中每行中出现最多的前5个单词,但是,这并不能解决我的问题。它给我的结果是-

[(' Trojan.PowerShell.LNK.Gen.2', 3),
(' Suspicious ZIP!lnk', 2),
(' HEUR:Trojan-Downloader.WinLNK.Powedon.a', 2),
(' TROJ_FR.8D496570', 2),
('Trojan.PowerShell.LNK.Gen.2', 1),
(' Trojan.PowerShell.LNK.Gen.2 (B)', 1),
(' Win32.Trojan-downloader.Powedon.Lrsa', 1),
(' PowerShell.DownLoader.466', 1),
(' malware (ai score=86)', 1),
(' Probably LNKScript', 1),
(' virus.lnk.powershell.a', 1),
(' Troj/LnkPS-A', 1),
(' Trojan.LNK', 1)]

然而,我想要像“特洛伊木马”,“下载程序”,“Powershell”。。。作为最重要的结果。你知道吗

匹配的单词可以是CSV中的值(单元格)的子字符串,也可以是两个或更多单词的组合。有人可以帮助解决这个问题,要么使用关键字列表或没有。你知道吗

谢谢!你知道吗


Tags: 文件csv方法字符串列表关键字zip单词
1条回答
网友
1楼 · 发布于 2024-07-03 07:58:47

让,my_values = ['A', 'B', 'C', 'A', 'Z', 'Z' ,'X' , 'A' ,'X','H','D' ,'A','S', 'A', 'Z']是要排序的单词列表。你知道吗

现在取一个列表,它将存储每个单词出现的信息。你知道吗

count_dict={}

用适当的值填充字典:

for i in my_values:
    if count_dict.get(i)==None: #If the value is not present in the dictionary then this is the first occurrence of the value
        count_dict[i]=1
    else:
        count_dict[i] = count_dict[i]+1 #If previously found then increment it's value

现在根据dict的出现情况对其值进行排序:

sorted_items= sorted(count_dict.items(),key=operator.itemgetter(1),reverse=True)

现在你有你的预期结果了! 最常见的3个值是:

print(sorted_items[:3])

输出:

[('A', 5), ('Z', 3), ('X', 2)]

最常见的两个值是:

print(sorted_items[:3])

输出:

[('A', 5), ('Z', 3)]

等等。你知道吗

相关问题 更多 >