python中用逗号分割输出_用逗号分割并在Python中分隔空格

本文介绍了在Python中如何使用列表理解、正则表达式和map函数来分割并去除字符串中的逗号和空格。通过实例展示了不同方法的使用和性能对比,包括使用strip()、re.split()以及lambda函数与map()的结合应用。
摘要由CSDN通过智能技术生成

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

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

请参阅: 关于列表理解的Python文档

列表理解的好的2秒的解释。

使用正则expression式分割。 请注意,我把这个例子与领先的空格进行了比较。 列表理解是去除前面和后面的空string。

>>> 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']

看到在领先的空间?

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

我知道这已经被回答了,但是如果你这样做了很多,正则expression式可能是更好的select:

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

\s匹配任何空白字符,我们只是用一个空string''replace它。 你可以在这里find更多的信息: http : //docs.python.org/library/re.html#re.sub

我来补充一下:

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

但是看到Jason Orendorff在评论中已经提到过它。

阅读格伦·梅纳德(Glenn Maynard)在同样的答案中提出的对地图列表理解的评论,我开始想知道为什么。 我认为他是出于performance的原因,但当然他可能是出于文体上的原因,或其他(格伦?)。

所以在一个循环中应用三种方法的快速(可能有缺陷?)testing显示:

[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(','))的胜利者,虽然看起来他们都在同一个球场。

当然,虽然地图(有或没有拉姆达)不应该排除出于性能的原因,对我来说,至less清晰的列表理解。

编辑:

Ubuntu 10.04上的Python 2.6.5

在分割之前,只需从string中删除空格。

mylist = my_string.replace(' ','').split(',')

import re result=[x for x in re.split(',| ',your_string) if x!='']

这对我来说工作得很好。

s = 'bla, buu, jii' sp = [] sp = s.split(',') for st in sp: print st

re (如在正则expression式中)允许一次分割多个字符:

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

这对你的示例string不起作用,但对逗号分隔的列表很好。 对于你的例子string,你可以结合re.split的function来分割正则expression式模式来获得“分裂这或那”的效果。

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

不幸的是,这是丑陋的,但filter将做到这一点:

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

瞧!

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

简单地说,逗号或至less有一个空白/有前/后空白。

请尝试!

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

map(lambda s: s.strip(), mylist)会比明确的循环好一点。

或者对于整个事情一次:

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

这基本上是你需要的一切。

从string中删除空格,然后您可以拆分它。

mylist = my_string.replace(' ','.').replace('.','').replace(', ',',').replace(' ,',',').split(',')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值