解析输出后获取两个单独列表的方法

时间:2013-10-17 07:23:16

标签: python

我希望从以下输出中获得2个单独的列表: -

>>> a = """
... ===================================================================
... IO Statistics
... Interval: 2.000 secs
... Column #0: COUNT(frame.time)frame.time
...                 |   Column #0
Time            |          COUNT
... Time            |          COUNT
... 000.000-002.000              1921
... 002.000-004.000              2000
... 004.000-006.000              1999
... 006.000-008.000              1999
... 008.000-010.000              1995
... 010.000-012.000              1997
... 012.000-014.000              1999
... 014.000-016.000              2001
... 016.000-018.000              2004
... 018.000-020.000              1995
... 020.000-022.000              1997
... 022.000-024.000              2007
... 024.000-026.000              2003
... 026.000-028.000              1998
... 028.000-030.000              1995
... 030.000-032.000              1994
... 032.000-034.000              2001
... 034.000-036.000              2008
... 036.000-038.000              1996
... 038.000-040.000              1996
... 040.000-042.000                95
... ===================================================================
... """

带输出的当前代码: -

>>> print re.findall(r'\s*(?P<first>\d+\.\d+)\-\d+\.\d+\s*(?P<id>\d+)\s*',a)
[('000.000', '1921'), ('002.000', '2000'), ('004.000', '1999'), ('006.000', '1999'), ('008.000', '1995'), ('010.000', '1997'), ('012.000', '1999'), ('014.000', '2001'), ('016.000', '2004'), ('018.000', '1995'), ('020.000', '1997'), ('022.000', '2007'), ('024.000', '2003'), ('026.000', '1998'), ('028.000', '1995'), ('030.000', '1994'), ('032.000', '2001'), ('034.000', '2008'), ('036.000', '1996'), ('038.000', '1996'), ('040.000', '95')]

这里我得到一个包含2个组合值的列表,但所需的输出是: -

['0','2','4','6','8',...,'38','40'] -> 1st list
['1241', '1272', '1315', '1371', '1195', '1299', '1305', '1391', '1463', '1454', '1392', '1438', '1362', '1491', '1392', '1422', '1425', '1486', '1449', '1487', '1402', '1420', '1330', '1458', '1420', '144'] -> 2nd list

如果有人可以提出一种方法来实现所需的输出,将会很有帮助。

1 个答案:

答案 0 :(得分:2)

使用zip(*..)将输出转置为两个单独的列表:

lst1, lst2 = zip(*re.findall(r'\s*(?P<first>\d+\.\d+)\-\d+\.\d+\s*(?P<id>\d+)\s*',a))

要获得lst1中值的整数部分,您需要先将它们解释为浮点数,然后将它们映射回仅舍入的值:

lst1 = [format(float(i), '.0f') for i in lst1]

演示:

>>> zip(*re.findall(r'\s*(?P<first>\d+\.\d+)\-\d+\.\d+\s*(?P<id>\d+)\s*',a))
[('000.000', '002.000', '004.000', '006.000', '008.000', '010.000', '012.000', '014.000', '016.000', '018.000', '020.000', '022.000', '024.000', '026.000', '028.000', '030.000', '032.000', '034.000', '036.000', '038.000', '040.000'), ('1921', '2000', '1999', '1999', '1995', '1997', '1999', '2001', '2004', '1995', '1997', '2007', '2003', '1998', '1995', '1994', '2001', '2008', '1996', '1996', '95')]
>>> lst1, lst2 = zip(*re.findall(r'\s*(?P<first>\d+\.\d+)\-\d+\.\d+\s*(?P<id>\d+)\s*',a))
>>> [format(float(i), '.0f') for i in lst1]
['0', '2', '4', '6', '8', '10', '12', '14', '16', '18', '20', '22', '24', '26', '28', '30', '32', '34', '36', '38', '40']
>>> lst2
('1921', '2000', '1999', '1999', '1995', '1997', '1999', '2001', '2004', '1995', '1997', '2007', '2003', '1998', '1995', '1994', '2001', '2008', '1996', '1996', '95')