如何折叠连续的分隔符?

发布于 2024-11-17 12:44:04 字数 371 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

↘紸啶 2024-11-24 12:44:04

这是尽可能简洁的:

string = 'aaa'
result = [s for s in string.split('a') if s]

或者您可以切换到正则表达式:

string = 'aaa'
result = re.split('a+', string)

This is about as concise as you can get:

string = 'aaa'
result = [s for s in string.split('a') if s]

Or you could switch to regular expressions:

string = 'aaa'
result = re.split('a+', string)
空城之時有危險 2024-11-24 12:44:04

您可以使用 re.split 以正则表达式作为分隔符,如下所示:

re.split(pattern, string[, maxsplit=0, flags=0])

You can use re.split with a regular expression as the delimiter, as in:

re.split(pattern, string[, maxsplit=0, flags=0])
倾城泪 2024-11-24 12:44:04

您可以使用正则表达式:

re.split(pattern, string[, maxsplit=0, flags=0])

as

re.split('a+', 'aaa')

You could use regular expressions:

re.split(pattern, string[, maxsplit=0, flags=0])

as

re.split('a+', 'aaa')
残疾 2024-11-24 12:44:04

我认为你的解决方案完全没问题。这是一个等效的:

filter(bool, 'aaa'.split('a'))

不过,可能不如列表理解那么明显。

I think your solution is perfectly OK. Here's an equivalent one:

filter(bool, 'aaa'.split('a'))

Probably not as perspicuous as a list comprehension, though.

如歌彻婉言 2024-11-24 12:44:04

不,没有更方便的方法了。您可以编写自己的分割函数,或者删除空字符串。

但你的解决方案看起来非常清晰和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.

趴在窗边数星星i 2024-11-24 12:44:04

尽管这并不完全符合您的要求,但您可以使用 set 删除所有非唯一标记:

>>> result
['', '', '']
>>> set(result)
set([''])

Although it's not exactly what you asked for, you can get rid of all non-unique tokens by using a set:

>>> result
['', '', '']
>>> set(result)
set([''])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文