将字符串拆分为两个单独的数字和字母列表-python

将字符串拆分为两个单独的数字和字母列表-python,python,regex,string,list,Python,Regex,String,List,标题似乎说明了一切。例如: 我想分手 stringtosplit = 'hello57world' 进入 然后把它们都放回弦里 letters = 'helloworld' numbers = '57' 有什么方法可以做到这一点吗?我希望我的代码尽可能简洁。数字和字母可以出现在字符串中的任何位置,空格和特殊字符已经被过滤掉。通常,您可以这样做: >>> stringtosplit = 'hello57world' >>> onlyLetter = "".

标题似乎说明了一切。例如: 我想分手

stringtosplit = 'hello57world' 
进入

然后把它们都放回弦里

letters = 'helloworld'
numbers = '57'

有什么方法可以做到这一点吗?我希望我的代码尽可能简洁。数字和字母可以出现在字符串中的任何位置,空格和特殊字符已经被过滤掉。

通常,您可以这样做:

>>> stringtosplit = 'hello57world'
>>> onlyLetter = "".join([i for i in stringtosplit if i.isalpha()])
>>> onlyLetter
'helloworld'
>>> onlyDig = "".join([i for i in stringtosplit if i.isdigit()])
>>> onlyDig
函数i.isalpha()将测试i是否为字母,i.isdigit()将测试i是否为数字。

使用和生成器理解:

>>> s = 'hello57world'
>>> alphas = ''.join(c for c in s if c.isalpha())
>>> nums = ''.join(c for c in s if c.isdigit())
>>> print alphas, nums
helloworld 57
用于检查变量是否为字母,以及是否为数字。然后使用
'.join(str)
列表转换为
str
使用正则表达式:

import re

stringtosplit = 'hello57world'
letters = ''.join(re.findall('([a-zA-Z])', stringtosplit))
numbers = ''.join(re.findall('([0-9])', stringtosplit))

工具:

>>> re.findall(r, 'hello57world')[0]  # your string 
('hello', '57', 'world')
>>> re.findall(r, 'hello57')[0]  # word after number ""
('hello', '57', '')
>>> re.findall(r, '3234abcd')[0] # word before number ""
('', '3234', 'abcd')
>>> re.findall(r, '450')[0]  # only number
('', '450', '')
>>> re.findall(r, 'hello')[0]  # number is ""
('hello', '', '')
>>> re.findall(r, '')[0] # empty string
('', '', '')
  • 您应该将此Python正则表达式与组一起使用。我相信这将提供最有效的清理方法:

    r = r"(?P<first>[a-z]*)(?P<num>[0-9]*)(?P<last>[a-z]*)"
    #           ^^^^^         ^^^^              ^^^
    #    before numbers      numbers        after numbers  
    # any group can be absent
    
    代码:
    现在,您可以用三行代码编写简单的代码:

    >>> stringtosplit = 'hello57world' 
    >>> r = r"(?P<first>[a-z]*)(?P<num>[0-9]*)(?P<last>[a-z]*)"
    >>> f, n, l = re.findall(r, stringtosplit)[0]
    >>> n
    '57'
    >>> f + l
    'helloworld'
    
    stringtosplit='hello57world' >>>r=r“(?P[a-z]*)(?P[0-9]*)(?P[a-z]*)” >>>f,n,l=re.findall(r,stringtosplit)[0] >>>n '57' >>>f+l “helloworld”

  • 试试看

    你有没有试过这样做?你迷路了吗?或者只是让别人帮你做?看看
    str.isalpha
    str.isdigit
    。我实际上在做之前就想到了aj8uppal的实现,但只是检查它是否是最短的方法。非常感谢!我想投你一票,但我没有足够的代表。我相信你有足够的声誉:)@aj8uppal:你是如何创造产出的?这是一个自动化的Python吗?或者你只是用
    >>
    ..
    和输出。。。?我不懂Python…如果这不明显;)我打开一个pythonshell,一行一行地输入。然后我把它复制过来粘贴在这里。通过进入命令提示符(OSX上的终端)并键入
    python
    打开python外壳。忘记添加,我可以使用
    re.I
    忽略大小写为
    re.findall(r,stringtosplit,re.I)[0]
    r = r"(?P<first>[a-z]*)(?P<num>[0-9]*)(?P<last>[a-z]*)"
    #           ^^^^^         ^^^^              ^^^
    #    before numbers      numbers        after numbers  
    # any group can be absent
    
    >>> re.findall(r, 'hello57world')[0]  # your string 
    ('hello', '57', 'world')
    >>> re.findall(r, 'hello57')[0]  # word after number ""
    ('hello', '57', '')
    >>> re.findall(r, '3234abcd')[0] # word before number ""
    ('', '3234', 'abcd')
    >>> re.findall(r, '450')[0]  # only number
    ('', '450', '')
    >>> re.findall(r, 'hello')[0]  # number is ""
    ('hello', '', '')
    >>> re.findall(r, '')[0] # empty string
    ('', '', '')
    
    >>> stringtosplit = 'hello57world' 
    >>> r = r"(?P<first>[a-z]*)(?P<num>[0-9]*)(?P<last>[a-z]*)"
    >>> f, n, l = re.findall(r, stringtosplit)[0]
    >>> n
    '57'
    >>> f + l
    'helloworld'