简体   繁体   English

在defaultdict(list)python中计数

[英]count in defaultdict(list) python

I am not able to get it working without your help. 没有你的帮助,我无法让它工作。 I want to filter some syslog messages based on devicename. 我想基于devicename过滤一些系统日志消息。 The output should look like this. 输出应该如下所示。

Device1: 1x failure1,50 x failure2, 20x failure3
Device3: 10 x failure1,5 x failure2, 2x failure3

Code: 码:

frequencies = defaultdict(list)

word = ['syslog1error1','syslog1error2','syslog1error3']

def findpattern():
    for line in syslog:
            if re.search(r"regexforhostname",line):
                hostname= line.strip()[16:27]
                for failure in word:
                    if failure in line:     
                    frequencies[hostname].append(failure)

x = findpattern()

print frequencies

Output looks like 输出看起来像

'Devicename':'syslog1error1', 'syslog1error1', 'syslog1error2', 'syslog1error3'

I would like to count the double entries in the list. 我想计算列表中的双重条目。 But I can't get it running with import collections (counter) 但我无法使用导入集合运行它(计数器)

Please help. 请帮忙。

Use collections.Counter() (see Counter in Collections module Python if you are on a Python version < 2.7): 使用collections.Counter() (如果您使用的是Python版本<2.7,请参阅Collections模块中的Counter ):

from collections import Counter, defaultdict

def findpattern():
    frequencies = defaultdict(Counter)

    for line in syslog:
        if re.search(r"regexforhostname",line):
            hostname= line.strip()[16:27]
            frequencies[hostname].update(f for f in word if f in line)

    return frequencies

result = findpattern()
for device, frequencies in result.iteritems():
    print '{}: {}'.format(
        device, 
        ', '.join(['{}x {}'.format(c, f) for f, c in frequencies.most_common()]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM