我需要计算不同的IDENTIFIERS并打印它们的数字计数。信息来自如下所示的数据流:
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的读取次数
答案 0 :(得分:3)
您可以使用collections.Counter
,但对于这种情况,collections.defaultdict
就足够了:
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])
答案 1 :(得分:1)
当然,您可以使用简单的gnu工具,例如
receivetest -f=/dev/pcan33 | cut -f 5 | uniq | wc -l
但如果你坚持使用python ......
identifiers = set(line.split()[4] for line in lines)
答案 2 :(得分:0)
如果您只想要一个字典来计算对象,您可以这样做:
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
之后您可以打印或访问该词典以查看您收到每个标识符的次数
答案 3 :(得分:0)
>>> 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}