如何在python中减去字符串

jay*_*y a 8 python string bioinformatics python-3.x

基本上,如果我有一个字符串'AJ'和另一个字符串'AJYF',我希望能够写'AJYF'-'AJ'和得到'YF'.

我试过这个但是语法错误了.

只是在旁注中,减法器总是比从中减去的字符串短.此外,减法器将始终类似于从中减去的字符串.例如,如果我有'GTYF'并且我想从中减去一个长度为3的字符串,那么该字符串必须是'GTY'.

如果可能的话,我想要做的全部功能是根据列表中每个项目的长度将字符串转换为列表.有没有办法做到这一点?

Shu*_*deo 15

简易解决方案是:

>>> string1 = 'AJYF'
>>> string2 = 'AJ'
>>> if string2 in string1:
...     string1.replace(string2,'')
'YF'
>>>
Run Code Online (Sandbox Code Playgroud)

  • 那个“if”检查是没有必要的。如果“string2”“不在 string1”中,则“replace”将不执行任何操作并返回原始字符串... (3认同)

bli*_*bli 8

replace 如果第二个字符串出现在多个位置,则可以执行您不想要的操作:

s1 = 'AJYFAJYF'
s2 = 'AJ'
if s1.startswith(s2):
    s3 = s1.replace(s2, '')
s3
# 'YFYF'
Run Code Online (Sandbox Code Playgroud)

你可以添加一个额外的参数来replace表明你只希望发生一个替换:

if s1.startswith(s2):
    s3 = s1.replace(s2, '', 1)
s3
# 'YFAJYF'
Run Code Online (Sandbox Code Playgroud)

或者您可以使用该re模块:

import re
if s1.startswith(s2):
    s3 = re.sub('^' + s2, '', s1)
s3
# 'YFAJYF'
Run Code Online (Sandbox Code Playgroud)

'^'是要确保s2它是仅在第一位置被取代s1

评论中建议的另一种方法是从 中取出第一个len(s2)字符s1

if s1.startswith(s2):
    s3 = s1[len(s2):] 
s3
# 'YFAJYF'
Run Code Online (Sandbox Code Playgroud)

在 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
Run Code Online (Sandbox Code Playgroud)


Tom*_*ron 6

我想你想要的是这个:

a = 'AJYF'
b = a.replace('AJ', '')
print a     # produces 'YF'
a = 'GTYF'
b = a.replace('GTY', '')
print a     # produces 'F'
Run Code Online (Sandbox Code Playgroud)