Python-re.splitstring

Python-re.splitstring,python,regex,list,split,Python,Regex,List,Split,我已经找了几个小时的解决办法了。我有一个变量要在嵌套列表中拆分 points ="""M445,346c28.8,0,56,11.2,76.4,31.6C541.8,398,553,425.2,553,454s-11.2,56-31.6,76.4C501,550.8,473.8,562,445,562 s-56-11.2-76.4-31.6C348.2,510,337,482.8,337,454s11.2-56,31.6-76.4S416.2,346,445,346 M445,3

我已经找了几个小时的解决办法了。我有一个变量要在嵌套列表中拆分

points ="""M445,346c28.8,0,56,11.2,76.4,31.6C541.8,398,553,425.2,553,454s-11.2,56-31.6,76.4C501,550.8,473.8,562,445,562
        s-56-11.2-76.4-31.6C348.2,510,337,482.8,337,454s11.2-56,31.6-76.4S416.2,346,445,346 M445,345c-60.2,0-109,48.8-109,109
        s48.8,109,109,109s109-48.8,109-109S505.2,345,445,345L445,345z"""

newPoints = re.split(r'[A-Za-z-]', points)
它是一个多行变量,具有svg文件中点的x和y位置

其模式是在一个字母处开始一个新项目。我想订购如下的东西。我试过上面的一些方法。邮件问题之一是它不断删除我的分隔符。:)


欢迎任何指点

您可以找到字母和浮点数,然后分组:

import re
import itertools
points ="""M445,346c28.8,0,56,11.2,76.4,31.6C541.8,398,553,425.2,553,454s-11.2,56-31.6,76.4C501,550.8,473.8,562,445,562
    s-56-11.2-76.4-31.6C348.2,510,337,482.8,337,454s11.2-56,31.6-76.4S416.2,346,445,346 M445,345c-60.2,0-109,48.8-109,109
    s48.8,109,109,109s109-48.8,109-109S505.2,345,445,345L445,345z"""
new_points = [list(b) for a, b in itertools.groupby(filter(None, re.findall('[a-zA-Z]+|[\d\.]+', points)), key=lambda x:re.findall('[a-zA-Z]+', x))]
final_data = [[new_points[i], [int(c) if re.findall('^\d+$', c) else float(c) for c in new_points[i+1]]] for i in range(0, len(new_points)-1, 2)]
输出:

[[['M'], [445, 346]], [['c'], [28.8, 0, 56, 11.2, 76.4, 31.6]], [['C'], [541.8, 398, 553, 425.2, 553, 454]], [['s'], [11.2, 56, 31.6, 76.4]], [['C'], [501, 550.8, 473.8, 562, 445, 562]], [['s'], [56, 11.2, 76.4, 31.6]], [['C'], [348.2, 510, 337, 482.8, 337, 454]], [['s'], [11.2, 56, 31.6, 76.4]], [['S'], [416.2, 346, 445, 346]], [['M'], [445, 345]], [['c'], [60.2, 0, 109, 48.8, 109, 109]], [['s'], [48.8, 109, 109, 109]], [['s'], [109, 48.8, 109, 109]], [['S'], [505.2, 345, 445, 345]], [['L'], [445, 345]]]

您可以查找字母和浮点数,然后分组:

import re
import itertools
points ="""M445,346c28.8,0,56,11.2,76.4,31.6C541.8,398,553,425.2,553,454s-11.2,56-31.6,76.4C501,550.8,473.8,562,445,562
    s-56-11.2-76.4-31.6C348.2,510,337,482.8,337,454s11.2-56,31.6-76.4S416.2,346,445,346 M445,345c-60.2,0-109,48.8-109,109
    s48.8,109,109,109s109-48.8,109-109S505.2,345,445,345L445,345z"""
new_points = [list(b) for a, b in itertools.groupby(filter(None, re.findall('[a-zA-Z]+|[\d\.]+', points)), key=lambda x:re.findall('[a-zA-Z]+', x))]
final_data = [[new_points[i], [int(c) if re.findall('^\d+$', c) else float(c) for c in new_points[i+1]]] for i in range(0, len(new_points)-1, 2)]
输出:

[[['M'], [445, 346]], [['c'], [28.8, 0, 56, 11.2, 76.4, 31.6]], [['C'], [541.8, 398, 553, 425.2, 553, 454]], [['s'], [11.2, 56, 31.6, 76.4]], [['C'], [501, 550.8, 473.8, 562, 445, 562]], [['s'], [56, 11.2, 76.4, 31.6]], [['C'], [348.2, 510, 337, 482.8, 337, 454]], [['s'], [11.2, 56, 31.6, 76.4]], [['S'], [416.2, 346, 445, 346]], [['M'], [445, 345]], [['c'], [60.2, 0, 109, 48.8, 109, 109]], [['s'], [48.8, 109, 109, 109]], [['s'], [109, 48.8, 109, 109]], [['S'], [505.2, 345, 445, 345]], [['L'], [445, 345]]]

哇!谢谢成功了!我已经更改了“最终数据”部分。它漏掉了最后一个(奇数)值。我已将其更改为:final_data=[]用于范围内的I(0,len(new_points),2):尝试:if new_points[I+1]:final_data.append([new_points[I],new_points[I+1]),但索引器除外:final_data.append([new_points[I]])@Tim很高兴提供帮助!哇!谢谢成功了!我已经更改了“最终数据”部分。它漏掉了最后一个(奇数)值。我已将其更改为:final_data=[]用于范围内的I(0,len(new_points),2):尝试:if new_points[I+1]:final_data.append([new_points[I],new_points[I+1]),但索引器除外:final_data.append([new_points[I]])@Tim很高兴提供帮助!