【问题标题】:Count how many times a string occurs in a specific column计算字符串在特定列中出现的次数
【发布时间】:2017-04-12 06:58:02
【问题描述】:

我试图查看字符串在第 4 列中出现了多少次。更具体地说,端口号在某些 Netflow 数据中出现了多少次。有数以千计的端口,所以除了递归之外,我没有寻找任何特定的东西。我已经使用冒号后面的数字解析到列中,我希望代码检查该数字出现了多少次,因此最终输出应该打印该数字以及它出现的次数......

[输出]

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

【问题讨论】:

  • 好的。你有什么问题?
  • 顺便说一句,你为什么用file.read for line in infile?这似乎在吠叫。
  • 最后的输出循环应该是:for port, count in d.items(): print("port:", port, "found:", count, "times.") - 如果你被困在 Python 2.7 上,请使用 iteritems
  • 我的问题是代码如何计算字符串在列 [4] 中出现的次数,而无需实际指定字符串是什么它只是查找任何字符串的递归并给出计数字符串是什么。
  • @rogernm001 来计算需要某种形式的东西来比较它的出现。 (如果有道理的话)

标签: python recursion count counter frequency


【解决方案1】:

你需要这样的东西:

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.get 将返回当前值或默认值(在本例中为 0)。

【讨论】:

  • 如何从高到低排序?
  • 这是一个单独的问题 - 几乎可以肯定是重复的
【解决方案2】:

来自 python 标准库。将返回一个包含您正在查找的内容的字典。

from collections import Counter
counts = Counter(column)
counts.most_common(n) # will return the most common values for specified number (n)

【讨论】:

  • 更多解释在这里会很有用。目前,我看不出这是如何回答问题的(这也不是真正的问题)。
  • 哦,天哪,我的问题是代码如何计算字符串在列中出现的次数[4],而无需实际指定字符串是什么,它只是查找任何字符串的递归并数一数。
  • 它应该将 1 行与文件中的所有其他行进行比较,并继续这样做,直到每行都被比较并计数是否有意义?
猜你喜欢
  • 2011-07-08
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
  • 2019-12-10
  • 2022-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多