gpt4 book ai didi

Python:在列表中拆分字符串的优雅且节省代码的方法

转载 作者:行者123 更新时间:2023-11-28 19:40:11 25 4
gpt4 key购买 nike

我有一个字符串:

mydata
'POINT (558750.3267372231900000 6361788.0628051758000000)'

我希望有一种代码保存方式来将列表数字转换为

(g, (x,y)) 

哪里:

g = geometry (POINT)
x = coordinates x
y = coordinates y

我正在使用

mydata.split(" ")
['POINT', '(558750.3267372231900000', '6361788.0628051758000000)']

但在那之后我需要使用几行代码来获取 x 和 y

最佳答案

一步一步:

>>> s = 'POINT (558750.3267372231900000 6361788.0628051758000000)'
>>> word, points = s.split(None, 1)
>>> word
'POINT'
>>> points
'(558750.3267372231900000 6361788.0628051758000000)'
>>> points = points.strip('()').split()
>>> points
['558750.3267372231900000', '6361788.0628051758000000']
>>> x, y = (float(i) for i in points)
>>> x
558750.3267372232
>>> y
6361788.062805176

关于Python:在列表中拆分字符串的优雅且节省代码的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13749324/

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