问题:第一次出现时分裂

在首次出现定界符时分割字符串的最佳方法是什么?

例如:

"123mango abcd mango kiwi peach"

首先分裂mango得到:

"abcd mango kiwi peach"

What would be the best way to split a string on the first occurrence of a delimiter?

For example:

"123mango abcd mango kiwi peach"

splitting on the first mango to get:

"abcd mango kiwi peach"

回答 0

文档

str.split([sep[, maxsplit]])

使用sep作为分隔符字符串,返回字符串中单词的列表。如果给出了maxsplit,则最多完成maxsplit分割(因此,列表中最多maxsplit+1包含元素)。

s.split('mango', 1)[1]

From the docs:

str.split([sep[, maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements).

s.split('mango', 1)[1]

回答 1

>>> s = "123mango abcd mango kiwi peach"
>>> s.split("mango", 1)
['123', ' abcd mango kiwi peach']
>>> s.split("mango", 1)[1]
' abcd mango kiwi peach'
>>> s = "123mango abcd mango kiwi peach"
>>> s.split("mango", 1)
['123', ' abcd mango kiwi peach']
>>> s.split("mango", 1)[1]
' abcd mango kiwi peach'

回答 2

对我来说,更好的方法是:

s.split('mango', 1)[-1]

…因为如果发生这种情况不在字符串中,您将得到“ IndexError: list index out of range"

因此-1不会造成任何伤害的原因发生次数已经设置为1。

For me the better approach is that:

s.split('mango', 1)[-1]

…because if happens that occurrence is not in the string you’ll get “IndexError: list index out of range".

Therefore -1 will not get any harm cause number of occurrences is already set to one.


回答 3

您也可以使用str.partition

>>> text = "123mango abcd mango kiwi peach"

>>> text.partition("mango")
('123', 'mango', ' abcd mango kiwi peach')

>>> text.partition("mango")[-1]
' abcd mango kiwi peach'

>>> text.partition("mango")[-1].lstrip()  # if whitespace strip-ing is needed
'abcd mango kiwi peach'

使用的优点str.partition是它总是会返回以下形式的元组:

(<pre>, <separator>, <post>)

因此,这使得解压缩输出变得非常灵活,因为在结果元组中总会有3个元素。

You can also use str.partition:

>>> text = "123mango abcd mango kiwi peach"

>>> text.partition("mango")
('123', 'mango', ' abcd mango kiwi peach')

>>> text.partition("mango")[-1]
' abcd mango kiwi peach'

>>> text.partition("mango")[-1].lstrip()  # if whitespace strip-ing is needed
'abcd mango kiwi peach'

The advantage of using str.partition is that it’s always gonna return a tuple in the form:

(<pre>, <separator>, <post>)

So this makes unpacking the output really flexible as there’s always going to be 3 elements in the resulting tuple.


回答 4

df.columnname[1].split('.', 1)

这将以第一个出现的“。”分割数据。在字符串或数据框列中的值。

df.columnname[1].split('.', 1)

This will split data with the first occurrence of ‘.’ in the string or data frame column value.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。