gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:34:29 24 4
gpt4 key购买 nike

我想从以下输出中获得 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

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

最佳答案

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

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

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

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

关于python - 解析输出后获取两个单独列表的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19420581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com