3 回答
TA贡献1818条经验 获得超3个赞
由于您的输入已经是一个字符串(而且您的预期结果也是?),您可以使用带有反向引用的正则表达式 来尝试这个超级懒惰的解决方案(([^,]+).*\2)。这里,[^,]+是第一个坐标对,.*其他对,\2再次是第一对。
>>> s = '17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875, 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094'
>>> re.findall(r"(([^,]+).*\2)", s)
[('17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875',
'17.17165756225586 -28.102264404296875'),
(' 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094',
' 28.865726470947266 -28.761619567871094')]
或者使用finditer并获取group直接获取字符串列表:
>>> [m.group() for m in re.finditer(r"(([^,]+).*\2)", s)]
['17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875',
' 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094']
一些后期处理后,获得对数字的实际列表(与_作为的结果findall;对于finditer删除[0]):
>>> [[tuple(map(float, y.split())) for y in x[0].split(",")] for x in _]
[[(17.17165756225586, -28.102264404296875),
(17.184370040893555, -28.200496673583984),
(17.1986083984375, -28.223613739013672),
(17.17165756225586, -28.102264404296875)],
[(28.865726470947266, -28.761619567871094),
(28.80694007873535, -28.75750160217285),
(28.792499542236328, -28.706947326660156),
(28.865726470947266, -28.761619567871094)]]
对于更长的字符串,这可能不是最快的解决方案,不过我没有计时。
TA贡献1804条经验 获得超3个赞
s='17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875, 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094'
#convert the input in a list of points
coordinates = [tuple(map(float,el.split())) for el in s.split(",")]
polygons = []
#find the polygons
while coordinates:
ind = coordinates[1:].index(coordinates[0])
polygons.append(coordinates[0:ind+2])
coordinates = coordinates[ind+2:]
#output
[(17.17165756225586, -28.102264404296875), (17.184370040893555, -28.200496673583984), (17.1986083984375, -28.223613739013672), (17.17165756225586, -28.102264404296875)]
[(28.865726470947266, -28.761619567871094), (28.80694007873535, -28.75750160217285), (28.792499542236328, -28.706947326660156), (28.865726470947266, -28.761619567871094)]
TA贡献1828条经验 获得超3个赞
这是一个相当丑陋但有效的解决方案,只是将明显的方法真正放入代码中。
# Note that your string has inconsistent separators -- sometimes ',', sometimes ', '.
# I'm going to separate on `,` and not worry about it -- you need to work out
# what the correct separator is.
s = '17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875, 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094'
coordinates = s.split(',')
polygon = []
polygons = []
new = True
for coordinate in coordinates:
polygon.append(coordinate)
if new:
start = coordinate
new = False
elif coordinate == start:
polygons.append(polygon)
polygon = []
new = True
result = [",".join(polygon) for polygon in polygons]
print(result)
Out:
['17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875', ' 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094']
添加回答
举报
