简体   繁体   English

如何在python中拆分字母数

[英]How to split numbers of letters in python

If I have a string like "MZA11LTE12DEP108N" which is a concatenation of letter groups and digit groups, how do I split them with a delimiter space character inbetween? 如果我有一个像“MZA11LTE12DEP108N”的字符串,它是字母组和数字组的串联,我如何用它们之间的分隔符空格字符拆分它们? in python 在python中

Assuming I'm understanding you, itertools.groupby could work: 假设我理解你, itertools.groupby可以工作:

>>> import itertools
>>> s = "MZA11LTE12DEP108N"
>>> [''.join(g) for k, g in itertools.groupby(s, str.isalpha)]
['MZA', '11', 'LTE', '12', 'DEP', '108', 'N']
>>> ' '.join(''.join(g) for k, g in itertools.groupby(s, str.isalpha))
'MZA 11 LTE 12 DEP 108 N'

Or a regex solution: 或正则表达式解决方案:

>>> import re
>>> s = "MZA11LTE12DEP108N"
>>> re.sub('(?<=\d)(?!\d)|(?<!\d)(?=\d)', ' ', s)
'MZA 11 LTE 12 DEP 108 N'

More verbosely: 更详细地说:

re.compile("""
    (?<=\d) # an empty string preceded by a digit
    (?!\d)  # followed by a non-digit
    |   # or
    (?<!\d) # an empty string preceded by a non-digit
    (?=\d)  # followed by a digit
""", re.VERBOSE).sub(' ', s)

well this is solution but it's so complicated :D , i've post it just as another solution for out of box LOL 这是解决方案,但它是如此复杂:D,我发布它只是作为开箱即用LOL的另一种解决方案

counter=0
txt='MZA11LTE12DEP108N'
string= ' '
while counter<len(txt):
    figure=str(txt[counter])
    if figure.isdigit():
        if string[-1].isdigit():
           string +=figure
        else:
            string += " "+figure
    else:
        if figure.isalpha():
            if string[-1].isalpha():string+=figure
            else:string += " "+figure

    counter+=1
string = string.strip()

If you mean that you want to separate characters in a variable, and digits in another then stick both of them with a space between them then: 如果你想要将变量中的字符和另一个中的数字分开,那么将两者中的数字分开,然后:

variable = "MZA11LTE12DEP108N"
varlist = list(variable)
num = len(varlist)
strings = ''
digits = ''
for i in range(num):
    if varlist[i].isalpha():
        strings += varlist[i]
    elif varlist[i].isdigit():
        digits += varlist[i]
variable = strings+' '+digits

Not sure if this is the best way. 不确定这是不是最好的方法。

For some reason I thought the intention was to remove non-numerics sigh hence my previous incorrect answer. 出于某种原因,我认为目的是消除非数字的叹息,因此我之前的错误答案。

In [473]: re.findall(r'(\D+|\d+)',"MZA11LTE12DEP108N")
Out[473]: ['MZA', '11', 'LTE', '12', 'DEP', '108', 'N']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM