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']
添加回答
舉報