亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

每秒出現一次未知元素后拆分字符串

每秒出現一次未知元素后拆分字符串

飲歌長嘯 2021-05-31 08:46:30
我有一個字符串,其中包含代表多邊形的坐標列表。在這個列表中,每個多邊形都有相同的起始和結束坐標。我需要將每個多邊形放在單獨的字符串(或列表)中。' 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 '所以從這個簡單的例子中,我需要有兩個元素:一個= ' 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 ' *字符串中可以有更多的多邊形,每個多邊形都需要分開。我只能為此使用標準的 python 庫
查看完整描述

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)]]

對于更長的字符串,這可能不是最快的解決方案,不過我沒有計時。


查看完整回答
反對 回復 2021-06-09
?
狐的傳說

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)]



查看完整回答
反對 回復 2021-06-09
?
倚天杖

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']



查看完整回答
反對 回復 2021-06-09
  • 3 回答
  • 0 關注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號