首页 文章

用逗号分隔并在Python中删除空格

提问于
浏览
275

我有一些python代码分裂逗号,但不剥离空格:

>>> string = "blah, lots  ,  of ,  spaces, here "
>>> mylist = string.split(',')
>>> print mylist
['blah', ' lots  ', '  of ', '  spaces', ' here ']

我宁愿最终删除这样的空格:

['blah', 'lots', 'of', 'spaces', 'here']

我知道我可以遍历列表和strip()每个项目,但是,因为这是Python,我猜测有更快,更简单,更优雅的方式 .

11 回答

  • 0
    import re
    mylist = [x for x in re.compile('\s*[,|\s+]\s*').split(string)
    

    简单地说,逗号或至少一个带/不带前/后空格的空格 .

    请试试!

  • 21

    map(lambda s: s.strip(), mylist) 会比显式循环好一点 .
    或者对于整个事情:

    map(lambda s:s.strip(), string.split(','))
    

    这基本上就是你需要的一切 .

  • 14

    使用列表理解 - 更简单,就像 for 循环一样容易阅读 .

    my_string = "blah, lots  ,  of ,  spaces, here "
    result = [x.strip() for x in my_string.split(',')]
    # result is ["blah", "lots", "of", "spaces", "here"]
    

    See: Python docs on List Comprehension
    A good 2 second explanation of list comprehension.

  • 2

    使用正则表达式拆分 . 注意我使用前导空格使案例更加通用 . 列表理解是删除前面和后面的空字符串 .

    >>> import re
    >>> string = "  blah, lots  ,  of ,  spaces, here "
    >>> pattern = re.compile("^\s+|\s*,\s*|\s+$")
    >>> print([x for x in pattern.split(string) if x])
    ['blah', 'lots', 'of', 'spaces', 'here']
    

    即使 ^\s+ 不匹配,这也有效:

    >>> string = "foo,   bar  "
    >>> print([x for x in pattern.split(string) if x])
    ['foo', 'bar']
    >>>
    

    这就是你需要^ \ s的原因:

    >>> pattern = re.compile("\s*,\s*|\s+$")
    >>> print([x for x in pattern.split(string) if x])
    ['  blah', 'lots', 'of', 'spaces', 'here']
    

    看到blah的领先空间?

    澄清:上面使用Python 3解释器,但结果在Python 2中是相同的 .

  • 471

    我来补充一下:

    map(str.strip, string.split(','))

    但是看到Jason Orendorff在a comment已经提到了它 .

    阅读格伦梅纳德在同一个答案中的评论,表明对 Map 的列表理解我开始想知道为什么 . 我认为他的出于性能原因,但当然他可能是出于文体原因或其他原因(格伦?) .

    所以在我的盒子上应用这三种方法的快速(可能有缺陷的?)测试显示:

    [word.strip() for word in string.split(',')]
    $ time ./list_comprehension.py 
    real    0m22.876s
    
    map(lambda s: s.strip(), string.split(','))
    $ time ./map_with_lambda.py 
    real    0m25.736s
    
    map(str.strip, string.split(','))
    $ time ./map_with_str.strip.py 
    real    0m19.428s
    

    map(str.strip, string.split(',')) 成为赢家,虽然看起来他们都在同一个球场 .

    当然,虽然出于性能原因,不一定要排除map(有或没有lambda),对我而言,它至少与列表理解一样清楚 .

    编辑:

    Ubuntu 10.04上的Python 2.6.5

  • 2

    我知道这已经得到了回答,但是如果你结束这么做,正则表达式可能是一个更好的方法:

    >>> import re
    >>> re.sub(r'\s', '', string).split(',')
    ['blah', 'lots', 'of', 'spaces', 'here']
    

    \s 匹配任何空格字符,我们只需用空字符串 '' 替换它 . 你可以在这里找到更多信息:http://docs.python.org/library/re.html#re.sub

  • 11

    在拆分之前,只需从字符串中删除空格 .

    mylist = my_string.replace(' ','').split(',')
    
  • 2
    s = 'bla, buu, jii'
    
    sp = []
    sp = s.split(',')
    for st in sp:
        print st
    
  • 1
    import re
    result=[x for x in re.split(',| ',your_string) if x!='']
    

    这对我来说很好 .

  • 1

    re (与正则表达式中一样)允许一次拆分多个字符:

    $ string = "blah, lots  ,  of ,  spaces, here "
    $ re.split(', ',string)
    ['blah', 'lots  ', ' of ', ' spaces', 'here ']
    

    这对于您的示例字符串不起作用,但适用于以逗号空间分隔的列表 . 对于您的示例字符串,您可以将re.split功能组合在正则表达式模式上进行拆分以获得"split-on-this-or-that"效果 .

    $ re.split('[, ]',string)
    ['blah',
     '',
     'lots',
     '',
     '',
     '',
     '',
     'of',
     '',
     '',
     '',
     'spaces',
     '',
     'here',
     '']
    

    不幸的是,这很难看,但是 filter 会做到这一点:

    $ filter(None, re.split('[, ]',string))
    ['blah', 'lots', 'of', 'spaces', 'here']
    

    瞧!

  • 9

    map(lambda s: s.strip(), mylist) 会比显式循环好一点 . 或者对于整个事情: map(lambda s:s.strip(), string.split(','))

相关问题