Python 我怎么能在没有嵌套循环的情况下用逗号分割呢。don';不要在标签内拆分

Python 我怎么能在没有嵌套循环的情况下用逗号分割呢。don';不要在标签内拆分,python,regex,Python,Regex,如何在没有嵌套循环的情况下使用逗号拆分? 不想在标签内部拆分。这些标签我不知道 r = ['aa,bb+,cc,[22]tt,df+,ar[/22]', 'cw+,di,ty,<KK>hy+,sb,dd<KK>'] for i in r: #print(i) lst = [] for l in re.split(r',\B', i): if l.startswith('[') or l.startswith('<'):

如何在没有嵌套循环的情况下使用逗号拆分? 不想在标签内部拆分。这些标签我不知道

r = ['aa,bb+,cc,[22]tt,df+,ar[/22]', 'cw+,di,ty,<KK>hy+,sb,dd<KK>']

for i in r:
    #print(i)
    lst = []
    for l in re.split(r',\B', i):
        if l.startswith('[') or l.startswith('<'):
            lst.append(l)
        else:
            lst.append(l.split(','))

r=['aa,bb+,cc,[22]tt,df+,ar[/22],'cw+,di,ty,hy+,sb,dd']
对于r中的i:
#印刷品(一)
lst=[]
对于拆分中的l(r',\B',i):

如果l.startswith('[')或l.startswith('我的解决方案是递归的:

pattern = re.compile('|'.join((
    r'(?P<PROCENT>(<%|\[)(?P<PROC>[0-9]{1,2})(\:|\])(?P<DATA>.+?)(>|\[/(?P=PROC)\]))', 
    r'(?P<SEP>\s*[;,]\s*)',
    r'(?P<VALUE>[a-zA-Z0-9]+\+?)'
)))

def range_parse(text: str, proc: float = 1.0):
    for m in pattern.finditer(text):
        lastgroup = m.lastgroup
        if lastgroup == 'SEP':
            continue
        elif lastgroup == 'PROCENT':
            yield from range_parse(m.group('DATA'), float(m.group('PROC')) / 100)
        elif ':' in m.group():
            data, proc = m.group().split(':')
            proc = float(proc)
        else:
            data = m.group()
        yield (lastgroup, data, proc)

pattern=re.compile('|'.join((
r'(?P(| \[/(?P=PROC)\])),
r'(?P\s*[;,]\s*),
r'(?P[a-zA-Z0-9]+\+?)
)))
定义范围解析(文本:str,过程:float=1.0):
对于模式中的m.finditer(文本):
lastgroup=m.lastgroup
如果lastgroup=='SEP':
持续
elif lastgroup=='PROCENT':
范围解析(m.group('DATA')、浮点(m.group('PROC'))/100的收益率
m.group()中的elif':'
数据,proc=m.group().split(“:”)
proc=浮动(proc)
其他:
数据=m.组()
收益率(最后一组、数据、过程)

预期的输出是什么?这是不清楚的,并且没有显示出解决问题的努力。在您的示例中,您始终有4个元素,因此可以使用
.split(“,”,3)
-
'aa,bb+,cc,[22]tt,df+,ar[/22]”。split(“,”,3)
'cw+,di,ty,hy+,sb,dd'。split(“,”,3)
pattern = re.compile('|'.join((
    r'(?P<PROCENT>(<%|\[)(?P<PROC>[0-9]{1,2})(\:|\])(?P<DATA>.+?)(>|\[/(?P=PROC)\]))', 
    r'(?P<SEP>\s*[;,]\s*)',
    r'(?P<VALUE>[a-zA-Z0-9]+\+?)'
)))

def range_parse(text: str, proc: float = 1.0):
    for m in pattern.finditer(text):
        lastgroup = m.lastgroup
        if lastgroup == 'SEP':
            continue
        elif lastgroup == 'PROCENT':
            yield from range_parse(m.group('DATA'), float(m.group('PROC')) / 100)
        elif ':' in m.group():
            data, proc = m.group().split(':')
            proc = float(proc)
        else:
            data = m.group()
        yield (lastgroup, data, proc)