python字符串剔除空格和逗号_用逗号分隔并在Python中删除空格
用逗號分隔并在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 2019-02-09T03:20:58Z
21 votes
使用正則表達式拆分。 注意我使用前導空格使案例更加通用。 列表理解是刪除前面和后面的空字符串。
>>> 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中是相同的。
tbc0 answered 2019-02-09T03:21:50Z
11 votes
我知道這已經得到了回答,但是如果你結束這么做,正則表達式可能是一個更好的方法:
>>> 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
我來補充一下:
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 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!='']
這對我來說很好。
Zieng answered 2019-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 2019-02-09T03:25:26Z
1 votes
map(lambda s: s.strip(), mylist)會比顯式循環好一點。 或者對于整個事情: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)
簡單地說,逗號或至少一個帶/不帶前/后空格的空格。
請試試!
GyuHyeon Choi answered 2019-02-09T03:26:26Z
0 votes
map(lambda s: s.strip(), mylist)會比顯式循環好一點。
或者對于整個事情:
map(lambda s:s.strip(), string.split(','))
這基本上就是你需要的一切。
DJbigpenis answered 2019-02-09T03:27:06Z
總結
以上是生活随笔為你收集整理的python字符串剔除空格和逗号_用逗号分隔并在Python中删除空格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019春招笔试
- 下一篇: Process On 免费在线作图工具