如何在python中减去字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42798967/
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
How to subtract strings in python
提问by jay a
Basically, if I have a string 'AJ'
and another string 'AJYF'
, I would like to be able to write 'AJYF'-'AJ'
and get 'YF'
.
基本上,如果我有一个 string'AJ'
和另一个 string 'AJYF'
,我希望能够编写'AJYF'-'AJ'
和获取'YF'
.
I tried this but got a syntax error.
我试过这个,但有一个语法错误。
Just on a side note the subtractor will always will be shorter than the string it is subtracted from. Also, the subtractor will always be like the string it is subtracted from. For instance, if I have 'GTYF' and I want to subtract a string of length 3 from it, that string has to be 'GTY'.
顺便提一下,减法器总是比减去它的字符串短。此外,减法器将始终类似于从中减去的字符串。例如,如果我有 'GTYF' 并且我想从中减去一个长度为 3 的字符串,那么该字符串必须是 'GTY'。
If it is possible, the full function I am trying to do is convert a string to a list based on how long each item in the list is supposed to be. Is there any way of doing that?
如果可能的话,我想要做的完整功能是根据列表中每个项目的长度将字符串转换为列表。有没有办法做到这一点?
回答by Shubham Namdeo
Easy Solution is:
简单的解决方案是:
>>> string1 = 'AJYF'
>>> string2 = 'AJ'
>>> if string2 in string1:
... string1.replace(string2,'')
'YF'
>>>
回答by Tom Barron
I think what you want is this:
我想你想要的是这个:
a = 'AJYF'
b = a.replace('AJ', '')
print a # produces 'YF'
a = 'GTYF'
b = a.replace('GTY', '')
print a # produces 'F'
回答by bli
replace
can do something that you do not want if the second string is present at several positions:
replace
如果第二个字符串出现在多个位置,则可以执行您不想要的操作:
s1 = 'AJYFAJYF'
s2 = 'AJ'
if s1.startswith(s2):
s3 = s1.replace(s2, '')
s3
# 'YFYF'
You can add an extra argument to replace
to indicate that you want only one replacement to happen:
你可以添加一个额外的参数来replace
表明你只希望发生一个替换:
if s1.startswith(s2):
s3 = s1.replace(s2, '', 1)
s3
# 'YFAJYF'
Or you could use the re
module:
或者您可以使用该re
模块:
import re
if s1.startswith(s2):
s3 = re.sub('^' + s2, '', s1)
s3
# 'YFAJYF'
The '^'
is to ensure that s2
it is substituted only at the first position of s1
.
的'^'
是要确保s2
它是仅在第一位置被取代s1
。
Yet another approach, suggested in the comments, would be to take out the first len(s2)
characters from s1
:
评论中建议的另一种方法是从 中取出第一个len(s2)
字符s1
:
if s1.startswith(s2):
s3 = s1[len(s2):]
s3
# 'YFAJYF'
Some tests using the %timeit magic in ipython (python 2.7.12, ipython 5.1.0) suggest that this last approach is faster:
在 ipython (python 2.7.12, ipython 5.1.0) 中使用 %timeit 魔法的一些测试表明最后一种方法更快:
In [1]: s1 = 'AJYFAJYF'
In [2]: s2 = 'AJ'
In [3]: %timeit s3 = s1[len(s2):]
The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 87.7 ns per loop
In [4]: %timeit s3 = s1[len(s2):]
The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 87.8 ns per loop
In [5]: %timeit s3 = s1[len(s2):]
The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 87.4 ns per loop
In [6]: %timeit s3 = s1.replace(s2, '', 1)
The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 230 ns per loop
In [7]: %timeit s3 = s1.replace(s2, '', 1)
The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 228 ns per loop
In [8]: %timeit s3 = s1.replace(s2, '', 1)
The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 234 ns per loop
In [9]: import re
In [10]: %timeit s3 = re.sub('^' + s2, '', s1)
The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 1.85 μs per loop
In [11]: %timeit s3 = re.sub('^' + s2, '', s1)
The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 1.86 μs per loop
In [12]: %timeit s3 = re.sub('^' + s2, '', s1)
The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 1.84 μs per loop
回答by moctarjallo
if you insist on using the '-' operator, then use a class with the __ sub__ dunder method overitten, with a combination of one of the solutions provided above:
如果您坚持使用 '-' 运算符,则使用 __ sub__ dunder 方法 overitten 的类,并结合上面提供的解决方案之一:
class String(object):
def __init__(self, string):
self.string = string
def __sub__(self, other):
if self.string.startswith(other.string):
return self.string[len(other.string):]
def __str__(self):
return self.string
sub1 = String('AJYF') - String('AJ')
sub2 = String('GTYF') - String('GTY')
print(sub1)
print(sub2)
It prints:
它打印:
YF
F