在字典中计数项目 [英] Count items in dictionary

查看:309
本文介绍了在字典中计数项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对不同的IDENTIFIERS进行计数,然后打印一个数字。信息来自一个如下所示的数据流:

I need to count the the different IDENTIFIERS and print a number count for the them. The information comes from a data stream that looks like this:

                             IDENTIFIER
7756753.940 receivetest: m s 0x0000069a 8 00 00 00 00 00 e4 8a 9a 
7756754.409 receivetest: m s 0x00000663 8 2f 00 62 02 00 e4 8a 9a 
7756754.878 receivetest: m s 0x0000069e 8 00 1f 5e 28 34 83 59 1a 
7756755.348 receivetest: m s 0x00000690 8 00 18 00 28 34 83 59 1a 
7756853.908 receivetest: m s 0x0000069a 8 00 00 00 00 00 e4 8a 9a 
7756854.377 receivetest: m s 0x0000069e 8 00 1f 5e 28 34 83 59 1a 
7756854.846 receivetest: m s 0x00000663 8 2f 00 62 02 00 e4 8a 9a 
7756855.316 receivetest: m s 0x00000690 8 00 18 00 28 34 83 59 1a 
7756953.961 receivetest: m s 0x0000069a 8 00 00 00 00 00 e4 8a 9a 
7756954.430 receivetest: m s 0x00000663 8 2f 00 62 02 00 e4 8a 9a 
7756954.857 receivetest: m s 0x0000069e 8 00 1f 5e 28 34 83 59 1a 
7756955.326 receivetest: m s 0x00000690 8 00 18 00 28 34 83 59 1a 
7757053.929 receivetest: m s 0x0000069a 8 00 00 00 00 00 e4 8a 9a 
7757054.398 receivetest: m s 0x00000663 8 2f 00 62 02 00 e4 8a 9a 
7757054.868 receivetest: m s 0x0000069e 8 00 1f 5e 28 34 83 59 1a 
7757055.337 receivetest: m s 0x00000690 8 00 18 00 28 34 83 59 1a 
7757153.940 receivetest: m s 0x0000069a 8 00 00 00 00 00 e4 8a 9a 
7757154.409 receivetest: m s 0x00000663 8 2f 00 62 02 00 e4 8a 9a 
7757154.878 receivetest: m s 0x0000069e 8 00 1f 5e 28 34 83 59 1a 
7757155.348 receivetest: m s 0x00000690 8 00 18 00 28 34 83 59 1a 
7757227.369 receivetest: m s 0x00000688 8 00 00 00 00 00 00 00 00 

这是我的代码:

#!/usr/bin/python

import subprocess
import re, os, pprint

DICT = {}

def RECEIVE(COMMAND):
    PROCESS = subprocess.Popen(COMMAND, stdout=subprocess.PIPE)
    LINES = iter(PROCESS.stdout.readline, "")
    for LINE in LINES:
        if re.match(r"^\d+.*$",LINE):
            SPLITLINE = LINE.split()
            del SPLITLINE[1:4]
            TIMER = SPLITLINE[0]
            IDENTIFIER = SPLITLINE[1]
            DLC = SPLITLINE[2]
            HEXBITS = SPLITLINE[3:]
            COUNTER = DICT.count(IDENTIFIER)
            DICT[IDENTIFIER] = [DLC, HEXBITS, TIMER[6:], COUNTER]
            for IDENTIFIER, HEXBITS in DICT.items():
                os.system("clear")
                pprint.pprint(DICT)

RECEIVE(["receivetest", "-f=/dev/pcan33"])

我只需要打印任何给定IDENTIFIER的读取次数

I just need to print the number of times any given IDENTIFIER has been read

推荐答案

你可以使用 collections.Counter ,但是一个 collections.defaultdict 对于这种情况就足够了:

You could use collections.Counter, but a collections.defaultdict will be sufficient for this case:

from collections import defaultdict

def RECEIVE(COMMAND):
    counts = defaultdict(int)

    # <your code>
    IDENTIFIER = SPLITLINE[1]
    counts[IDENTIFIER] += 1
    # <rest of your code>

    # <whenever you want to see how many times some identifier has appeared>
    print(counts[SOME_IDENTIFIER])

这篇关于在字典中计数项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆