用逗号分隔并在Python中删除空格
我有一些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个解决方案
433 votes
使用列表理解 - 更简单,和for循环一样容易阅读。
my_string = "blah, lots , of , spaces, here "
result = [x.strip() for x in my_string.split(',')]
# result is ["blah", "lots", "of", "spaces", "here"]
请参阅:List Comprehension上的Python文档
列表理解的2秒解释。
Sean Vieira answered -02-09T03:20:58Z
21 votes
使用正则表达式拆分。 注意我使用前导空格使案例更加通用。 列表理解是删除前面和后面的空字符串。
>>> import re
>>> string = " blah, lots , of , spaces, here "
>>> pattern = pile("^\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 = pile("\s*,\s*|\s+$")
>>> print([x for x in pattern.split(string) if x])
[' blah', 'lots', 'of', 'spaces', 'here']
看到blah的领先空间?
澄清:上面使用Python 3解释器,但结果在Python 2中是相同的。
tbc0 answered -02-09T03:21:50Z
11 votes
我知道这已经得到了回答,但是如果你结束这么做,正则表达式可能是一个更好的方法:
>>> import re
>>> re.sub(r'\s', '', string).split(',')
['blah', 'lots', 'of', 'spaces', 'here']
\s匹配任何空格字符,我们只需用空字符串''替换它。您可以在此处找到更多信息:[/library/re.html#re.sub]
Brad Montgomery answered -02-09T03:22:22Z
11 votes
我来补充一下:
map(str.strip, string.split(','))
但看到Jason Orendorff在评论中已经提到过它。
阅读格伦梅纳德在同一个答案中的评论,表明对地图的列表理解我开始想知道为什么。 我认为他的出于性能原因,但当然他可能是出于文体原因或其他原因(格伦?)。
所以在我的盒子上应用这三种方法的快速(可能有缺陷的?)测试显示:
[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
Sean answered -02-09T03:23:35Z
7 votes
在拆分之前,只需从字符串中删除空格。
mylist = my_string.replace(' ','').split(',')
user489041 answered -02-09T03:24:02Z
2 votes
s = 'bla, buu, jii'
sp = []
sp = s.split(',')
for st in sp:
print st
Parikshit Pandya answered -02-09T03:24:20Z
2 votes
import re
result=[x for x in re.split(',| ',your_string) if x!='']
这对我来说很好。
Zieng answered -02-09T03:24:44Z
2 votes
filter(与正则表达式中一样)允许一次拆分多个字符:
$ string = "blah, lots , of , spaces, here "
$ re.split(', ',string)
['blah', 'lots ', ' of ', ' spaces', 'here ']
这对于您的示例字符串不起作用,但适用于以逗号空间分隔的列表。 对于您的示例字符串,您可以将re.split功能组合在正则表达式模式上进行拆分,以获得“拆分此或那个”效果。
$ re.split('[, ]',string)
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
不幸的是,这很难看,但是filter可以解决这个问题:
$ filter(None, re.split('[, ]',string))
['blah', 'lots', 'of', 'spaces', 'here']
瞧!
Dannid answered -02-09T03:25:26Z
1 votes
map(lambda s: s.strip(), mylist)会比显式循环好一点。 或者对于整个事情:map(lambda s:s.strip(), string.split(','))
user470379 answered -02-09T03:25:53Z
1 votes
import re
mylist = [x for x in pile('\s*[,|\s+]\s*').split(string)
简单地说,逗号或至少一个带/不带前/后空格的空格。
请试试!
GyuHyeon Choi answered -02-09T03:26:26Z
0 votes
map(lambda s: s.strip(), mylist)会比显式循环好一点。
或者对于整个事情:
map(lambda s:s.strip(), string.split(','))
这基本上就是你需要的一切。
DJbigpenis answered -02-09T03:27:06Z