简体   繁体   English

使用什么数据结构来表示条形图

[英]What data structure to use to represent a bar graph

Question is really wider than the title allows me to specify. 问题的确超出了标题允许我指定的范围。 I have a big file representing unordered numbered packets in the order they were received and the timestamp that corresponds to it, such as (arrows included for clarity, not really in file): 我有一个大文件,按接收顺序和与之对应的时间戳表示无序编号的数据包,例如(为清楚起见,包括箭头,但实际上不在文件中):

seq_1 ----> timestamp seq_1 ---->时间戳

seq_2 ----> timestamp seq_2 ---->时间戳

seq_3 ----> timestamp seq_3 ---->时间戳

seq_2 ----> timestamp seq_2 ---->时间戳

seq_5 ----> timestamp seq_5 ---->时间戳

seq_4 ----> timestamp seq_4 ---->时间戳

... ...

Timestamps always increase, but I might duplicate packets, packets out of order, etc. I have parsed the file to a list of strings, and must now decide the appropriate data structure to save it, taking into account that I need to: 时间戳总是会增加,但是我可能会重复复制数据包,使数据包乱序等。我已将文件解析为字符串列表,并且现在必须考虑到我需要确定适当的数据结构以保存它:

  1. Remove all duplicated sequence numbers, only keeping the first one which arrived. 删除所有重复的序列号,仅保留第一个到达的序列号。
  2. Obtain an ordered iterable structure sorted by the sequence number. 获取按序列号排序的有序可迭代结构。

The idea is that I could plot (not really going to do it, though) a graph bar, x axis being the sequence numbers and y axis being the timestamp. 我的想法是,我可以绘制(尽管实际上不打算这样做)图形条,x轴是序列号,y轴是时间戳。 I need to manually find local maxima and minima, so I should be able to access the adjacent entries of any entry. 我需要手动查找局部最大值和最小值,因此我应该能够访问任何条目的相邻条目。

I have thought of parsing the list of lines to a dictionary of (sequence_number, timestamp) , carefully not overwriting existing entries (condition 1), then turning it into a list of tuple s and finally sorting the list by key . 我考虑过将行列表解析为(sequence_number, timestamp)dictionary ,小心地不覆盖现有条目(条件1),然后将其转换为tuple list最后按key对list进行排序 list should allow me to access adjacent entries, thus fulfilling condition 2. The parsed file is quite big, so I was wondering if there is a solution which would scale better (not requiring conversion between two data structures + posterior sorting). list应该允许我访问相邻的条目,从而满足条件2。解析的文件很大,因此我想知道是否存在一种可以更好地扩展的解决方案 (不需要在两个数据结构之间转换+后验排序)。

The easiest bet is to just dump things into a dictionary and sort the keys at the end. 最简单的选择是将内容转储到字典中并在最后对键进行排序。 The d.get call ensures that it keeps the first encountered value if one exists, or inserts a new value if it doesn't. d.get调用可确保它保留第一个遇到的值(如果存在的话),或者插入不存在的新值。

In [23]: s = """seq_1 ----> timestamp1
   ....: seq_2 ----> timestamp2
   ....: seq_3 ----> timestamp3
   ....: seq_2 ----> timestamp4
   ....: seq_5 ----> timestamp5
   ....: seq_4 ----> timestamp6
   ....: seq_9 ----> timestamp7
   ....: seq_10 ----> timestamp8
   ....: seq_6 ----> timestamp9
   ....: seq_7 ----> timestamp10
   ....: seq_2 ----> timestamp11
   ....: seq_4 ----> timestamp12"""

In [24]: d = {}

In [25]: for line in s.split("\n"):
    seq, ts = map(str.strip, line.split("---->"))
    d[seq] = d.get(seq, ts)
   ....:

In [26]: sorted(d.items(), key=lambda x: int(x[0][4:]))
Out[26]:
[('seq_1', 'timestamp1'),
 ('seq_2', 'timestamp2'),
 ('seq_3', 'timestamp3'),
 ('seq_4', 'timestamp6'),
 ('seq_5', 'timestamp5'),
 ('seq_6', 'timestamp9'),
 ('seq_7', 'timestamp10'),
 ('seq_9', 'timestamp7'),
 ('seq_10', 'timestamp8')]

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

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