Python 仅在字符串末尾删除子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3663450/
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
Remove substring only at the end of string
提问by l--''''''---------''''''''''''
I have a bunch of strings, some of them have ' rec'. I want to remove that only if those are the last 4 characters.
我有一堆字符串,其中一些有' rec'. 只有当这些是最后 4 个字符时,我才想删除它。
So in other words I have
所以换句话说,我有
somestring = 'this is some string rec'
and I want it to become
我希望它成为
somestring = 'this is some string'
What is the Python way to approach this?
解决这个问题的 Python 方法是什么?
采纳答案by Hyman Kelly
def rchop(s, suffix):
if suffix and s.endswith(suffix):
return s[:-len(suffix)]
return s
somestring = 'this is some string rec'
rchop(somestring, ' rec') # returns 'this is some string'
回答by Andrew Hare
You could use a regular expression as well:
您也可以使用正则表达式:
from re import sub
str = r"this is some string rec"
regex = r"(.*)\srec$"
print sub(regex, r"", str)
回答by Alex Martelli
Since you have to get len(trailing)anyway (where trailingis the string you want to remove IF it's trailing), I'd recommend avoiding the slight duplication of work that .endswithwould cause in this case. Of course, the proof of the code is in the timing, so, let's do some measurement (naming the functions after the respondents proposing them):
由于len(trailing)无论如何你都必须得到(trailing如果它在尾随,你想删除的字符串在哪里),我建议避免.endswith在这种情况下导致的轻微重复工作。当然,代码的证明在时间上,所以,让我们做一些测量(以受访者提出的函数命名):
import re
astring = 'this is some string rec'
trailing = ' rec'
def andrew(astring=astring, trailing=trailing):
regex = r'(.*)%s$' % re.escape(trailing)
return re.sub(regex, r'', astring)
def Hyman0(astring=astring, trailing=trailing):
if astring.endswith(trailing):
return astring[:-len(trailing)]
return astring
def Hyman1(astring=astring, trailing=trailing):
regex = r'%s$' % re.escape(trailing)
return re.sub(regex, '', astring)
def alex(astring=astring, trailing=trailing):
thelen = len(trailing)
if astring[-thelen:] == trailing:
return astring[:-thelen]
return astring
Say we've named this python file a.pyand it's in the current directory; now, ...:
假设我们已经命名了这个 python 文件a.py并且它在当前目录中;现在, ...:
$ python2.6 -mtimeit -s'import a' 'a.andrew()'
100000 loops, best of 3: 19 usec per loop
$ python2.6 -mtimeit -s'import a' 'a.Hyman0()'
1000000 loops, best of 3: 0.564 usec per loop
$ python2.6 -mtimeit -s'import a' 'a.Hyman1()'
100000 loops, best of 3: 9.83 usec per loop
$ python2.6 -mtimeit -s'import a' 'a.alex()'
1000000 loops, best of 3: 0.479 usec per loop
As you see, the RE-based solutions are "hopelessly outclassed" (as often happens when one "overkills" a problem -- possibly one of the reasons REs have such a bad rep in the Python community!-), though the suggestion in @Hyman's comment is way better than @Andrew's original. The string-based solutions, as expected, shing, with my endswith-avoiding one having a miniscule advantage over @Hyman's (being just 15% faster). So, both pure-string ideas are good (as well as both being concise and clear) -- I prefer my variant a little bit only because I am, by character, a frugal (some might say, stingy;-) person... "waste not, want not"!-)
如您所见,基于 RE 的解决方案“无可救药地被超越”(经常发生在“过度处理”一个问题时——这可能是 RE 在 Python 社区中拥有如此糟糕代表的原因之一!-),尽管在@Hyman 的评论比 @Andrew 的原评论要好得多。正如预期的那样,基于字符串的解决方案非常好,我的endswith-avoiding解决方案比@Hyman 的优势微乎其微(仅快 15%)。所以,两个纯字符串的想法都很好(而且都简洁明了)——我更喜欢我的变体,只是因为从性格上看,我是一个节俭(有人可能会说,吝啬;-)的人.. 。 “浪费不可”!-)
回答by Tony Veijalainen
As kind of one liner generator joined:
作为一种线性发生器加入:
test = """somestring='this is some string rec'
this is some string in the end word rec
This has not the word."""
match = 'rec'
print('\n'.join((line[:-len(match)] if line.endswith(match) else line)
for line in test.splitlines()))
""" Output:
somestring='this is some string rec'
this is some string in the end word
This has not the word.
"""
回答by Per Mejdal Rasmussen
If speed is not important, use regex:
如果速度不重要,请使用正则表达式:
import re
somestring='this is some string rec'
somestring = re.sub(' rec$', '', somestring)
回答by pylang
Using more_itertools, we can rstripstrings that pass a predicate.
使用more_itertools,我们可以rstrip传递一个谓词的字符串。
Installation
安装
> pip install more_itertools
Code
代码
import more_itertools as mit
iterable = "this is some string rec".split()
" ".join(mit.rstrip(iterable, pred=lambda x: x in {"rec", " "}))
# 'this is some string'
" ".join(mit.rstrip(iterable, pred=lambda x: x in {"rec", " "}))
# 'this is some string'
Here we pass all trailing items we wish to strip from the end.
在这里,我们传递了我们希望从末尾剥离的所有尾随项目。
See also the more_itertoolsdocsfor details.
有关详细信息,另请参阅more_itertools文档。
回答by user6663257
use:
用:
somestring.rsplit(' rec')[0]
回答by y2k-shubham
Taking inspiration from @David Foster's answer, I would do
从获得灵感@大卫·福斯特的回答,我会做
def _remove_suffix(text, suffix):
if text is not None and suffix is not None:
return text[:-len(suffix)] if text.endswith(suffix) else text
else:
return text
Reference: Pythonstring slicing
参考:Python字符串切片
回答by cdiggins
Here is a one-liner version of Hyman Kelly's answer along with its sibling:
这是Hyman凯利的答案及其兄弟姐妹的单行版本:
def rchop(s, sub):
return s[:-len(sub)] if s.endswith(sub) else s
def lchop(s, sub):
return s[len(sub):] if s.startswith(sub) else s
回答by Ehsan Ahmadi
def remove_trailing_string(content, trailing):
"""
Strip trailing component `trailing` from `content` if it exists.
"""
if content.endswith(trailing) and content != trailing:
return content[:-len(trailing)]
return content

