gpt4 book ai didi

Python 一个列表中的两个列表,带 Tab

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:06 25 4
gpt4 key购买 nike

我有一个列表:

data = ['Other stuff',186.797\t-48',
'187.272\t-48',
'187.747\t-48',
'188.222\t-45',
'188.697\t-43',
'189.172\t6',
'189.646\t-23',
'190.121\t12',...]

如何创建两个列表,一个在“/t”之前有值,另一个在“/t”之后有值。

但是,我只想从 data[1] 到 len(data) 跳过第一个值,因为它只是描述性文本。

例如:

L1 = [186.797,...,190.121]
L2 = [-48,..., 12]

最佳答案

您可以使用正则表达式:

import re
s = ['186.797\t-48',
'187.272\t-48',
'187.747\t-48',
'188.222\t-45',
'188.697\t-43',
'189.172\t6',
'189.646\t-23',
'190.121\t12']

L1, L2 = map(list, zip(*[[float(re.findall('[\d\.]+', i)[0]), int(re.findall('[\-\d]+$', i)[0])] for i in s]))

输出:

[186.797, 187.272, 187.747, 188.222, 188.697, 189.172, 189.646, 190.121]
[-48, -48, -48, -45, -43, 6, -23, 12]

关于Python 一个列表中的两个列表,带 Tab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49020187/

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