Python 计算字符串在特定列中出现的次数
我试图查看字符串在第4列中出现了多少次。更具体地说,端口号在某些Netflow数据中出现的次数。有数千个端口,所以除了递归之外,我不寻找任何特定的端口。我已经用冒号后面的数字解析了这个列,我想让代码检查这个数字出现的次数,这样最终的输出应该打印出这个数字出现的次数 [输出]Python 计算字符串在特定列中出现的次数,python,recursion,count,counter,frequency,Python,Recursion,Count,Counter,Frequency,我试图查看字符串在第4列中出现了多少次。更具体地说,端口号在某些Netflow数据中出现的次数。有数千个端口,所以除了递归之外,我不寻找任何特定的端口。我已经用冒号后面的数字解析了这个列,我想让代码检查这个数字出现的次数,这样最终的输出应该打印出这个数字出现的次数 [输出] Port: 80 found: 3 times. Port: 53 found: 2 times. Port: 21 found: 1 times. [守则] import re frequency = {} fil
Port: 80 found: 3 times.
Port: 53 found: 2 times.
Port: 21 found: 1 times.
[守则]
import re
frequency = {}
file = open('/Users/rojeliomaestas/Desktop/nettest2.txt', 'r')
with open('/Users/rojeliomaestas/Desktop/nettest2.txt', 'r') as infile:
next(infile)
for line in infile:
data = line.split()[4].split(":")[1]
text_string = file.read().lower()
match_pattern = re.findall(data, text_string)
for word in match_pattern:
count = frequency.get(word,0)
frequency[word] = count + 1
frequency_list = frequency.keys()
for words in frequency_list:
print ("port:", words,"found:", frequency[words], "times.")
[文件]
Date first seen Duration Proto Src IP Addr:Port Dst IP Addr:Port Packets Bytes Flows
2017-04-02 12:07:32.079 9.298 UDP 8.8.8.8:80 -> 205.166.231.250:8080 1 345 1
2017-04-02 12:08:32.079 9.298 TCP 8.8.8.8:53 -> 205.166.231.250:80 1 75 1
2017-04-02 12:08:32.079 9.298 TCP 8.8.8.8:80 -> 205.166.231.250:69 1 875 1
2017-04-02 12:08:32.079 9.298 TCP 8.8.8.8:53 -> 205.166.231.250:443 1 275 1
2017-04-02 12:08:32.079 9.298 UDP 8.8.8.8:80 -> 205.166.231.250:23 1 842 1
2017-04-02 12:08:32.079 9.298 TCP 8.8.8.8:21 -> 205.166.231.250:25 1 146 1
来自python标准库。将返回一本字典,其中包含您要查找的内容
from collections import Counter
counts = Counter(column)
counts.most_common(n) # will return the most common values for specified number (n)
你需要像这样的东西:
frequency = {}
with open('/Users/rojeliomaestas/Desktop/nettest2.txt', 'r') as infile:
next(infile)
for line in infile:
port = line.split()[4].split(":")[1]
frequency[port] = frequency.get(port,0) + 1
for port, count in frequency.items():
print("port:", port, "found:", count, "times.")
这项工作的核心是保留一个要计数的端口的dict,并为每一行增加它
dict.get
将返回当前值或默认值(在本例中为0)。确定。您的问题是什么?顺便问一下,为什么要使用文件。请阅读和以获取infle中的行
?这似乎很糟糕。最后的输出循环应该是:对于端口,在d中计数。items():print(“port:,port,“found:,count,“times”)
-如果您仍然使用Python 2.7,请使用iteritems
。我的问题是,代码如何计算字符串在列[4]中出现的次数无需实际指定字符串是什么,它只需查找任何字符串的递归,并给出一个计数以及字符串是什么。@rogernm001要计算某个事件的发生次数,它需要某种形式的某个事件进行比较。(如果这有意义的话)更多的解释在这里是有用的。目前,我看不出这是如何回答这个问题的(这也不是一个真正的问题)。哦,天哪,我的问题是,代码如何计算字符串在列中出现的次数[4]不必指定字符串是什么,它只需查找任何字符串的递归并给出一个计数。它应该将1行与文件中的所有其他行进行比较,并一直这样做,直到每一行都进行了比较和计数(如果有意义的话)?我如何将其从大到小排序?这是一个单独的问题-几乎可以肯定是一个问题复制品