日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python字符串剔除空格和逗号_用逗号分隔并在Python中删除空格

發(fā)布時(shí)間:2024/3/24 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python字符串剔除空格和逗号_用逗号分隔并在Python中删除空格 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

用逗號(hào)分隔并在Python中刪除空格

我有一些python代碼分裂逗號(hào),但不剝離空格:

>>> string = "blah, lots , of , spaces, here "

>>> mylist = string.split(',')

>>> print mylist

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

我寧愿最終刪除這樣的空格:

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

我知道我可以遍歷列表和strip()每個(gè)項(xiàng)目,但是,因?yàn)檫@是Python,我猜測(cè)有更快,更簡(jiǎn)單,更優(yōu)雅的方式。

11個(gè)解決方案

433 votes

使用列表理解 - 更簡(jiǎn)單,和for循環(huán)一樣容易閱讀。

my_string = "blah, lots , of , spaces, here "

result = [x.strip() for x in my_string.split(',')]

# result is ["blah", "lots", "of", "spaces", "here"]

請(qǐng)參閱:List Comprehension上的Python文檔

列表理解的2秒解釋。

Sean Vieira answered 2019-02-09T03:20:58Z

21 votes

使用正則表達(dá)式拆分。 注意我使用前導(dǎo)空格使案例更加通用。 列表理解是刪除前面和后面的空字符串。

>>> 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的領(lǐng)先空間?

澄清:上面使用Python 3解釋器,但結(jié)果在Python 2中是相同的。

tbc0 answered 2019-02-09T03:21:50Z

11 votes

我知道這已經(jīng)得到了回答,但是如果你結(jié)束這么做,正則表達(dá)式可能是一個(gè)更好的方法:

>>> import re

>>> re.sub(r'\s', '', string).split(',')

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

\s匹配任何空格字符,我們只需用空字符串''替換它。您可以在此處找到更多信息:[http://docs.python.org/library/re.html#re.sub]

Brad Montgomery answered 2019-02-09T03:22:22Z

11 votes

我來(lái)補(bǔ)充一下:

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

但看到Jason Orendorff在評(píng)論中已經(jīng)提到過(guò)它。

閱讀格倫梅納德在同一個(gè)答案中的評(píng)論,表明對(duì)地圖的列表理解我開始想知道為什么。 我認(rèn)為他的出于性能原因,但當(dāng)然他可能是出于文體原因或其他原因(格倫?)。

所以在我的盒子上應(yīng)用這三種方法的快速(可能有缺陷的?)測(cè)試顯示:

[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(','))獲勝者,雖然看起來(lái)他們都在同一個(gè)球場(chǎng)。

當(dāng)然,雖然出于性能原因,不一定要排除map(有或沒(méi)有l(wèi)ambda),對(duì)我而言,它至少與列表理解一樣清楚。

編輯:

Ubuntu 10.04上的Python 2.6.5

Sean answered 2019-02-09T03:23:35Z

7 votes

在拆分之前,只需從字符串中刪除空格。

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

user489041 answered 2019-02-09T03:24:02Z

2 votes

s = 'bla, buu, jii'

sp = []

sp = s.split(',')

for st in sp:

print st

Parikshit Pandya answered 2019-02-09T03:24:20Z

2 votes

import re

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

這對(duì)我來(lái)說(shuō)很好。

Zieng answered 2019-02-09T03:24:44Z

2 votes

filter(與正則表達(dá)式中一樣)允許一次拆分多個(gè)字符:

$ string = "blah, lots , of , spaces, here "

$ re.split(', ',string)

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

這對(duì)于您的示例字符串不起作用,但適用于以逗號(hào)空間分隔的列表。 對(duì)于您的示例字符串,您可以將re.split功能組合在正則表達(dá)式模式上進(jìn)行拆分,以獲得“拆分此或那個(gè)”效果。

$ re.split('[, ]',string)

['blah',

'',

'lots',

'',

'',

'',

'',

'of',

'',

'',

'',

'spaces',

'',

'here',

'']

不幸的是,這很難看,但是filter可以解決這個(gè)問(wèn)題:

$ filter(None, re.split('[, ]',string))

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

瞧!

Dannid answered 2019-02-09T03:25:26Z

1 votes

map(lambda s: s.strip(), mylist)會(huì)比顯式循環(huán)好一點(diǎn)。 或者對(duì)于整個(gè)事情:map(lambda s:s.strip(), string.split(','))

user470379 answered 2019-02-09T03:25:53Z

1 votes

import re

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

簡(jiǎn)單地說(shuō),逗號(hào)或至少一個(gè)帶/不帶前/后空格的空格。

請(qǐng)?jiān)囋?#xff01;

GyuHyeon Choi answered 2019-02-09T03:26:26Z

0 votes

map(lambda s: s.strip(), mylist)會(huì)比顯式循環(huán)好一點(diǎn)。

或者對(duì)于整個(gè)事情:

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

這基本上就是你需要的一切。

DJbigpenis answered 2019-02-09T03:27:06Z

總結(jié)

以上是生活随笔為你收集整理的python字符串剔除空格和逗号_用逗号分隔并在Python中删除空格的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。