Count items in dictionary

只谈情不闲聊 提交于 2019-12-11 16:14:38

问题


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 

Here is my code:

#!/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"])

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


回答1:


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])



回答2:


You could, of course, use simple gnu tools like

receivetest -f=/dev/pcan33 | cut -f 5 | uniq | wc -l

But if you insist on using python...

identifiers = set(line.split()[4] for line in lines)



回答3:


If you just want a dictionary to count objects you could do something like this:

id_count = {}
def recieve():
    # insert your subprocess and read here, change the loop
    for line in identifiers.split('\n'):
    if re.match(r"^\d+.*$",line):
        identifier = line.split()[4]
        if identifier in id_count.keys():
            id_count[identifier] += 1
        else:
            id_count[identifier] = 1

afterwards you can print or access the dict to see how many times you received each identifier




回答4:


>>> DICT = {}
>>> for LINE in LINES:
...   SPLITLINE = LINE.split()
...   try:
...      DICT[SPLITLINE[4]] = DICT[SPLITLINE[4]] + 1
...   except:
...      DICT[SPLITLINE[4]] = 1
... 
>>> DICT
{'0x0000069a': 5, '0x00000690': 5, '0x00000688': 1, '0x00000663': 5, '0x0000069e': 5}


来源:https://stackoverflow.com/questions/27623756/count-items-in-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!