Python 在空格或连字符上拆分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16926870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split on either a space or a hyphen?
提问by Richard
In Python, how do I split on either a space or a hyphen?
在 Python 中,如何拆分空格或连字符?
Input:
输入:
You think we did this un-thinkingly?
Desired output:
期望的输出:
["You", "think", "we", "did", "this", "un", "thinkingly"]
I can get as far as
我可以达到
mystr.split(' ')
But I don't know how to split on hyphens as well as spaces and the Python definition of split only seems to specify a string. Do I need to use a regex?
但我不知道如何在连字符和空格上进行拆分,而且 Python 的 split 定义似乎只指定了一个 string。我需要使用正则表达式吗?
采纳答案by Elazar
If your pattern is simple enough for one (or maybe two) replace, use it:
如果您的模式对于一个(或两个)来说足够简单replace,请使用它:
mystr.replace('-', ' ').split(' ')
Otherwise, use RE as suggested by @jamylak.
否则,请按照@jamylak 的建议使用 RE 。
回答by jamylak
>>> import re
>>> text = "You think we did this un-thinkingly?"
>>> re.split(r'\s|-', text)
['You', 'think', 'we', 'did', 'this', 'un', 'thinkingly?']
As @larsmans noted, to split by multiple spaces/hyphens (emulating .split()with no arguments) used [...]for readability:
正如@larsmans 所指出的,为了可读性,要由多个空格/连字符(.split()不带参数模拟)分割[...]:
>>> re.split(r'[\s-]+', text)
['You', 'think', 'we', 'did', 'this', 'un', 'thinkingly?']
Without regex (regex is the most straightforward option in this case):
没有正则表达式(在这种情况下,正则表达式是最直接的选择):
>>> [y for x in text.split() for y in x.split('-')]
['You', 'think', 'we', 'did', 'this', 'un', 'thinkingly?']
Actually the answer by @Elazarwithout regex is quite straightforward as well (I would still vouch for regex though)
实际上,@Elazar没有使用正则表达式的答案也很简单(不过我仍然保证使用正则表达式)
回答by James
A regex is far easier and better, but if you're staunchly opposed to using one:
正则表达式要简单得多,也更好,但如果您坚决反对使用正则表达式:
import itertools
itertools.chain.from_iterable((i.split(" ") for i in myStr.split("-")))

