如何折叠连续的分隔符?
Python 中默认的 split 方法将连续空格视为单个分隔符。但是,如果您指定分隔符字符串,则连续分隔符不会折叠:
>>> 'aaa'.split('a')
['', '', '', '']
折叠连续分隔符的最直接方法是什么?我知道我可以从结果列表中删除空字符串:
>>> result = 'aaa'.split('a')
>>> result
['', '', '', '']
>>> result = [item for item in result if item]
但是有更方便的方法吗?
The default split
method in Python treats consecutive spaces as a single delimiter. But if you specify a delimiter string, consecutive delimiters are not collapsed:
>>> 'aaa'.split('a')
['', '', '', '']
What is the most straightforward way to collapse consecutive delimiters? I know I could just remove empty strings from the result list:
>>> result = 'aaa'.split('a')
>>> result
['', '', '', '']
>>> result = [item for item in result if item]
But is there a more convenient way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是尽可能简洁的:
或者您可以切换到正则表达式:
This is about as concise as you can get:
Or you could switch to regular expressions:
您可以使用
re.split
以正则表达式作为分隔符,如下所示:You can use
re.split
with a regular expression as the delimiter, as in:您可以使用正则表达式:
as
You could use regular expressions:
as
我认为你的解决方案完全没问题。这是一个等效的:
不过,可能不如列表理解那么明显。
I think your solution is perfectly OK. Here's an equivalent one:
Probably not as perspicuous as a list comprehension, though.
不,没有更方便的方法了。您可以编写自己的分割函数,或者删除空字符串。
但你的解决方案看起来非常清晰和Pythonic。
No, there isn't a more convenient way. Either you write your own split function, or do the removing empty strings.
But your solution seems very clear and pythonic.
尽管这并不完全符合您的要求,但您可以使用
set
删除所有非唯一标记:Although it's not exactly what you asked for, you can get rid of all non-unique tokens by using a
set
: