使用 python 将列表拆分成对
Split a list into pairs using python
我有一个list/string。我想把它分成两对,并在与输出相同的列表中用括号将它们分开。我该怎么做?
到目前为止我尝试了什么?
ip='MDSYS.SDO_GEOMETRY(2003, NULL, NULL, MDSYS.SDO_ELEM_INFO_ARRAY(1, 1003, 1), MDSYS.SDO_ORDINATE_ARRAY(22027, 22943.23, 22026, 22939, 22025, 22936, 22025.09, 22932, 22027, 22929, 22030, 22926)'
split_string_1 = "MDSYS.SDO_ORDINATE_ARRAY("
split_string_2 = ")"
data = list(map(int, ip.split(split_string_1)[1].split(split_string_2)[0].split(", ")))
result = list(zip(data[:-1], data[1:]))
我收到一条错误消息 ValueError: invalid literal for int() with base 10: '22943.23'
我该如何解决这个问题?
期望的输出:
[(22027, 22943.23), (22026, 22939), (22025, 22936), (22025.09, 22932), (22027, 22929), (22030, 22926)]
您可以 rpartition
2 次使用分隔符:
>>> out = ip.rpartition(split_string_1)[-1].rpartition(split_string_2)[0]
>>> out
"22027, 22943.23, 22026, 22939, 22025, 22936, 22025.09, 22932, 22027, 22929, 22030, 22926"
然后split
over ", "
and map
to float
s; lastly take every 2 elements with zip
(即奇数索引和偶数索引并行)形成输出:
>>> out = list(map(float, out.split(", ")))
>>> out = list(zip(out[::2], out[1::2]))
>>> out
[(22027.0, 22943.23),
(22026.0, 22939.0),
(22025.0, 22936.0),
(22025.09, 22932.0),
(22027.0, 22929.0),
(22030.0, 22926.0)]
你差不多搞定了,只需将 int 替换为 float,因为你的数据中有浮点数
data = list(map(float, ip.split(split_string_1)[1].split(split_string_2)[0].split(", ")))
result = list(zip(data[::2], data[1::2]))
print(result)
>> [(22027.0, 22943.23), (22943.23, 22026.0), (22026.0, 22939.0), (22939.0, 22025.0), (22025.0, 22936.0), (22936.0, 22025.09), (22025.09, 22932.0), (22932.0, 22027.0), (22027.0, 22929.0), (22929.0, 22030.0), (22030.0, 22926.0)]
对你分手的评论:
如果你保证有那种形式的字符串,你也许可以做以下事情:
1)去掉最后一个)
字符,ip = ip[:-1]
2)拆分'('并取最后一部分,ip = ip.split("(")[-1]
3) 用逗号分隔,ip = ip.split(",")
ip = ip[:-1].split("(")[-1].split(",")
data = list(map(float, ip))
result = list(zip(data[::2], data[1::2]))
使用正则表达式模式 ('\([0-9., ]+\)
,它将为您提供包含 interger/float 的所有元组,使用 ast.literal_eval()
将字符串转换为元组。最后使用 list(zip(out[::2], out[1::2])
获取元组列表
import ast
import re
out = re.findall('\([0-9., ]+\)', ip)[-1]
out = ast.literal_eval(out)
out = list(zip(out[::2], out[1::2])
print(out)
[(22027, 22943.23),
(22026, 22939),
(22025, 22936),
(22025.09, 22932),
(22027, 22929),
(22030, 22926)]
我建议您定义一个执行操作的方法,在本例中它返回一个生成器:
def each_slice(iterable, n=2):
if n < 2: n = 1
i, size = 0, len(iterable)
while i < size-n+1:
yield iterable[i:i+n]
i += n
获得列表后(不考虑字符串到数字的转换):
lst = ['22027', '22943.23', '22026', '22939', '22025', '22936', '22025.09', '22932', '22027', '22929', '22030', '22926']
你可以直接调用方法 each_slice(lst)
:
print(list(each_slice(lst)))
#=> [['22027', '22943.23'], ['22026', '22939'], ['22025', '22936'], ['22025.09', '22932'], ['22027', '22929'], ['22030', '22926']]
请注意,此实现切断了提醒元素,例如按五个分组:
print(list(each_slice(lst, n=5)))
#=> [['22027', '22943.23', '22026', '22939', '22025'], ['22936', '22025.09', '22932', '22027', '22929']]
我有一个list/string。我想把它分成两对,并在与输出相同的列表中用括号将它们分开。我该怎么做?
到目前为止我尝试了什么?
ip='MDSYS.SDO_GEOMETRY(2003, NULL, NULL, MDSYS.SDO_ELEM_INFO_ARRAY(1, 1003, 1), MDSYS.SDO_ORDINATE_ARRAY(22027, 22943.23, 22026, 22939, 22025, 22936, 22025.09, 22932, 22027, 22929, 22030, 22926)'
split_string_1 = "MDSYS.SDO_ORDINATE_ARRAY("
split_string_2 = ")"
data = list(map(int, ip.split(split_string_1)[1].split(split_string_2)[0].split(", ")))
result = list(zip(data[:-1], data[1:]))
我收到一条错误消息 ValueError: invalid literal for int() with base 10: '22943.23'
我该如何解决这个问题?
期望的输出:
[(22027, 22943.23), (22026, 22939), (22025, 22936), (22025.09, 22932), (22027, 22929), (22030, 22926)]
您可以 rpartition
2 次使用分隔符:
>>> out = ip.rpartition(split_string_1)[-1].rpartition(split_string_2)[0]
>>> out
"22027, 22943.23, 22026, 22939, 22025, 22936, 22025.09, 22932, 22027, 22929, 22030, 22926"
然后split
over ", "
and map
to float
s; lastly take every 2 elements with zip
(即奇数索引和偶数索引并行)形成输出:
>>> out = list(map(float, out.split(", ")))
>>> out = list(zip(out[::2], out[1::2]))
>>> out
[(22027.0, 22943.23),
(22026.0, 22939.0),
(22025.0, 22936.0),
(22025.09, 22932.0),
(22027.0, 22929.0),
(22030.0, 22926.0)]
你差不多搞定了,只需将 int 替换为 float,因为你的数据中有浮点数
data = list(map(float, ip.split(split_string_1)[1].split(split_string_2)[0].split(", ")))
result = list(zip(data[::2], data[1::2]))
print(result)
>> [(22027.0, 22943.23), (22943.23, 22026.0), (22026.0, 22939.0), (22939.0, 22025.0), (22025.0, 22936.0), (22936.0, 22025.09), (22025.09, 22932.0), (22932.0, 22027.0), (22027.0, 22929.0), (22929.0, 22030.0), (22030.0, 22926.0)]
对你分手的评论:
如果你保证有那种形式的字符串,你也许可以做以下事情:
1)去掉最后一个)
字符,ip = ip[:-1]
2)拆分'('并取最后一部分,ip = ip.split("(")[-1]
3) 用逗号分隔,ip = ip.split(",")
ip = ip[:-1].split("(")[-1].split(",")
data = list(map(float, ip))
result = list(zip(data[::2], data[1::2]))
使用正则表达式模式 ('\([0-9., ]+\)
,它将为您提供包含 interger/float 的所有元组,使用 ast.literal_eval()
将字符串转换为元组。最后使用 list(zip(out[::2], out[1::2])
import ast
import re
out = re.findall('\([0-9., ]+\)', ip)[-1]
out = ast.literal_eval(out)
out = list(zip(out[::2], out[1::2])
print(out)
[(22027, 22943.23),
(22026, 22939),
(22025, 22936),
(22025.09, 22932),
(22027, 22929),
(22030, 22926)]
我建议您定义一个执行操作的方法,在本例中它返回一个生成器:
def each_slice(iterable, n=2):
if n < 2: n = 1
i, size = 0, len(iterable)
while i < size-n+1:
yield iterable[i:i+n]
i += n
获得列表后(不考虑字符串到数字的转换):
lst = ['22027', '22943.23', '22026', '22939', '22025', '22936', '22025.09', '22932', '22027', '22929', '22030', '22926']
你可以直接调用方法 each_slice(lst)
:
print(list(each_slice(lst)))
#=> [['22027', '22943.23'], ['22026', '22939'], ['22025', '22936'], ['22025.09', '22932'], ['22027', '22929'], ['22030', '22926']]
请注意,此实现切断了提醒元素,例如按五个分组:
print(list(each_slice(lst, n=5)))
#=> [['22027', '22943.23', '22026', '22939', '22025'], ['22936', '22025.09', '22932', '22027', '22929']]