为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

饮歌长啸 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 关注
  • 206 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号