字典中的棘手列表操作

时间:2016-04-21 07:10:32

标签: python python-3.x

我在文件中有以下数据。我想从相关的行中提取timesize并绘制时间序列图。

    03/12 20:23:26.11: 04:23:26 L9 <Mx  Acc  Magnum All            XDV:00111A0000000117 00D3001200870172 01FF6000F01CFE81 3D26000000000300
    03/12 20:23:26.11: 04:23:26 L9 <Mx  Acc  MID 0x1500 Len 26   XDV:00111A0000000117 00D3001200870172 01FF6000F01CFE81 3D26000000000300
    03/12 20:23:26.11: 04:23:26 L8 <Mx  JK31 (Mx)                  JSP:17.37.6.99: Size = 166, Data: 00345C4101003031 E463EF0113108701 5A01FF6008F01CFE 81AB170000000003 EF01131087015A01 FF6008F01CFE81AB 170000000003EF01 131087015B01FF60 00F01CFE81701B00 00000003EF011310 87015B01FF6000F0 1CFE81701B000000 0003EF0113108701 5C01FF2000F01CFE 81CB240000000003 EF01131087015C01 57CC00F01CFE81CB 240000000003EF01 131087015D01FF20 00F01CFE815B2900 00000003EF011310 87015D01FF2000F0 1CFE815B29000000 0003EF0113108701 5E01FF6000F01CFE 819D280000000003 EF01131087015E01 FF6000F01CFE819D 0003
    03/15 20:23:26.11: 04:23:26 L8 <Kx  JK31 (Kx)                  JSP:15.33.2.93: Size = 163, Data: 00647741000030EF 01131087015A01FF 6008F01CFE81AB17 0000000003EF0113 1087015A01FF6008 F01CFE81AB170000 000003EF01131087 015B01FF6000F01C FE81701B00000000 03EF01131087015B 01FF6000F01CFE81 701B0000000003EF 01131087015C01FF 2000F01CFE81CB24 0000000003EF0113 1087015C01FF2000 F01CFE81CB240000 000003EF01131087 015D01FF2000F01C FE815B2900000000 03EF01131087015D 01FF2000F01CFE81 5B290000000003EF 01131087015E01FF 6000F01CFE819D28 0000000003EF0113 1087015E01FF6000 F01CFE819D280000 A6220000000003
    03/15 20:23:26.11: 04:23:26 L9 <Kx  JK31 (Kx)                  JSP:10.22.1.53:Size = 163, Data: 009D1141000030EF 01131087015A01FF 6008F01CFE81AB17 0000000003EF0113 1087015A01FF6008 F01CFE81AB170000 000003EF01131087 015B01FF6000F01C FE81701B00000000 03EF01131087015B 01FF6000F01CFE81 701B0000000003EF 01131087015C01FF 2000F01CFE81CB24 0000000003EF0113 1087015C01FF2000 F01CFE81CB240000 000003EF01131087 015D01FF2000F01C FE815B2900000000 03EF01131087015D 01FF2000F01CFE81 5B290000000003EF 01131087015E01FF 6000F01CFE819D28 0000000003EF0113 1087015E01FF6000 F01CFE819D280000 A6220000000003

我有以下程序来做。

from dateutil import parser

import matplotlib.pyplot as plt

match_list = ["L8 <Mx JK31 (Mx)", "L9 <Mx JK31 (Mx)"]  ## put all match strings in this list
with open("test.txt") as fin:
    print(' : {}', fin.name)
    time_data = {}  ## save data in dictionaries, with string keys and lists as values
    size_data = {}
    for line in fin:
        for match in match_list:
            if match in line:
               if match not in time_data:
                   time_data[match] = []  ## initialize empty list the first time this key is encountered
                   size_data[match] = []
               line = line.strip.split()
               time_str = line[2]
               t = parser.parse(time_str)  
               time_data[match].append(t)
               size = int(line[9].strip(","))
               size_data[match].append(size)


    for match in match_list:
        plt.figure()  ## create a new figure for each data set
        plt.plot(time_data[match], size_data[match])
    plot.show()  ## simultaneously show all plots

我上面使用了两个词典time_datasize_data。每个数据都包含match_list的元素作为其关键字。 values是包含日期时间对象的列表。

完成上述操作后,使用matplotlib进行绘图很容易。 现在我想做以下几点。

正如您在上面的示例数据中看到的相同的密钥L8 <Mx JK31 (Mx),您有两个具有相同时间的值(04:23:26)。

我希望以这样的方式修改数据结构(即我的词典中的列表),以便我希望每分钟对大小值(即字典size_data内的列表中的值)进行求和。 / p>

假设有5个值如下

04:23:26 56 04:23:26 60 04:23:43 70 04:23:46 80 04:23:56 90

我希望将上述内容替换为04:23:00356。我该怎么做呢。

1 个答案:

答案 0 :(得分:1)

我相信你的问题比起初看起来要容易一些,因为解析文件需要额外的开销。我假设解析工作,你最终得到一个列表元组,dicts ... 现在你想在那个列表上执行一种聚合,对吗?

所以,从

开始
[
['10:10:01', 45],
['10:10:11', 135],
['10:10:50', 21],
['10:10:57', 4],
['10:11:01', 2],
['10:11:11', 8]
]

你想要

[
['10:10:00', 205],
['10:11:00' 10]
]

如果是这样,你可以轻松地使用defaultdict和datetime.replace(seconds = 0)。

此代码不是即插即用的,但您应该能够很容易地适应您的情况

input = # your parser function
output = defaultdict(int)

for date, value in input.items():
  output[date.replace(seconds=0)] += value

如果您想再次使用列表,可以使用output.items()

祝你好运!