Python 基于开始索引和结束索引删除字符串

Python 基于开始索引和结束索引删除字符串,python,Python,因此,我有一堆长字符串,因此我想到了一种有效的方法来完成这个操作 假设我有一根类似 "< stuff to remove> get this stuff <stuff to remove> “获取此内容 所以,我试着提取“得到这些东西” 所以我写了这样的东西 strt_pos = 0 end_pos = 0 while True: strt_idx = string.find(start_point, strt_pos) # start_point =

因此,我有一堆长字符串,因此我想到了一种有效的方法来完成这个操作 假设我有一根类似

 "< stuff to remove> get this stuff <stuff to remove>
获取此内容
所以,我试着提取“得到这些东西”

所以我写了这样的东西

 strt_pos = 0
  end_pos = 0
 while True:
   strt_idx = string.find(start_point, strt_pos) # start_point = "<" in our example
   end_idx  = string.find(end_point, end_pos)   # end_point = ">" in our example
   chunk_to_remove = string[strt_idx:end_idx]
    # Now how do i chop this part off from the string??
   strt_pos = strt_pos + 1
    end_pos = end_pos + 1
   if str_pos >= len(string) # or maybe end_pos >= len(string):
      break
strt\u pos=0
结束位置=0
尽管如此:
strt_idx=string.find(在我们的示例中是start_point,strt_pos)#start_point=”“
chunk\u to\u remove=string[strt\u idx:end\u idx]
#现在我该如何从弦上切掉这部分??
strt\U pos=strt\U pos+1
结束位置=结束位置+1
如果str_pos>=len(字符串)或end_pos>=len(字符串):
打破

实现此功能的更好方法是什么使用正则表达式:

>>> s = "< stuff to remove> get this stuff <stuff to remove>"
>>> import re
>>> re.sub(r'<[^<>]*>', '', s)
' get this stuff '
>>s=“获取此内容”
>>>进口稀土
>>>关于子条款(r''、''、s)
“拿这个东西”
表达式
匹配以
开头的字符串,并且它们之间既没有
。然后
命令将匹配项替换为空字符串,从而将其删除

然后,如果需要,可以对结果调用
.strip()
,以删除前导空格和尾随空格


当然,当您有嵌套标记时,这将失败,但它适用于您的示例。

使用正则表达式:

>>> s = "< stuff to remove> get this stuff <stuff to remove>"
>>> import re
>>> re.sub(r'<[^<>]*>', '', s)
' get this stuff '
>>s=“获取此内容”
>>>进口稀土
>>>关于子条款(r''、''、s)
“拿这个东西”
表达式
匹配以
开头的字符串,并且它们之间既没有
。然后
命令将匹配项替换为空字符串,从而将其删除

然后,如果需要,可以对结果调用
.strip()
,以删除前导空格和尾随空格


当然,例如,当您有嵌套的标记时,这将失败,但它将适用于您的示例。

正则表达式将是实现这一点的简单方法(尽管不一定如jedwards的回答所示更快):

重新导入
s=“获取此内容”
s=re.sub(r']*>,'',s)

在这之后,
s
将是字符串
'get this stuff'

正则表达式将是一种简单的方法(尽管不一定像jedwards的回答所示更快):

重新导入
s=“获取此内容”
s=re.sub(r']*>,'',s)

在这个
s
之后将是字符串
'get this stuff'

我不确定您正在执行的搜索操作是否是问题的一部分。如果您只是说您有一个开始索引和结束索引,并且希望从字符串中删除这些字符,那么您不需要特殊的函数。Python允许您对字符串中的字符使用数字索引

> x="abcdefg"
> x[1:3]
'bc'

要执行的操作类似于
x[:strt\u idx]+x[end\u idx:][/code>(如果省略第一个参数,则表示“从头开始”,如果省略第二个参数,则表示“继续到结束”。)

我不确定您正在执行的搜索操作是否是问题的一部分。如果您只是说您有一个开始索引和结束索引,并且希望从字符串中删除这些字符,则不需要特殊的函数。Python允许您对字符串中的字符使用数字索引

> x="abcdefg"
> x[1:3]
'bc'

要执行的操作类似于
x[:strt\u idx]+x[end\u idx:][/code>(如果省略第一个参数,则表示“从头开始”,如果省略第二个参数,则表示“继续到结尾”)

如果有字符串的起始和结束索引,则可以执行以下操作:

substring = string[s_ind:e_ind]
其中,
s_ind
是要包含在字符串中的第一个字符的索引,
e_ind
是不希望包含在字符串中的第一个字符的索引

比如说

string = "Long string of which I only want a small part"
#         012345678901234567890123456789012345678901234
#         0         1         2         3
substring = string[21:32]
print substring
打印
我只想要

你可以用和现在一样的方式找到索引


Edit:关于效率,这种类型的解决方案实际上比正则表达式解决方案的效率更高。原因是正则表达式中包含了大量开销,而您不一定需要这些开销

我鼓励你自己测试这些东西,而不是盲目地去做人们认为最有效的事情

考虑以下测试程序:

#!/bin/env python

import re
import time

def inner_regex(s):
    return re.sub(r'<[^>]*>', '', s)

def inner_substr(s):
    s_ind = s.find('>') + 1
    e_ind = s.find('<', s_ind)
    return s[s_ind:e_ind]


s = '<stuff to remove> get this stuff <stuff to remove>'

tr1 = time.time()
for i in range(100000):
    s1 = inner_regex(s)
tr2 = time.time()
print("Regex:     %f" % (tr2 - tr1))

ts1 = time.time()
for i in range(100000):
    s2 = inner_substr(s)
ts2 = time.time()
print("Substring: %f" % (ts2 - ts1))
换言之,使用正则表达式方法,您的速度比您原来的纠正方法慢3倍多


编辑:关于已编译正则表达式的注释,它比未编译正则表达式快,但仍然比显式子字符串慢:

#!/bin/env python

import re
import time

def inner_regex(s):
    return re.sub(r'<[^>]*>', '', s)

def inner_regex_compiled(s,r):
    return r.sub('', s)

def inner_substr(s):
    s_ind = s.find('>') + 1
    e_ind = s.find('<', s_ind)
    return s[s_ind:e_ind]


s = '<stuff to remove> get this stuff <stuff to remove>'


tr1 = time.time()
for i in range(100000):
    s1 = inner_regex(s)
tr2 = time.time()


tc1 = time.time()
r = re.compile(r'<[^>]*>')
for i in range(100000):
    s2 = inner_regex_compiled(s,r)
tc2 = time.time()


ts1 = time.time()
for i in range(100000):
    s3 = inner_substr(s)
ts2 = time.time()


print("Regex:          %f" % (tr2 - tr1))
print("Regex Compiled: %f" % (tc2 - tc1))
print("Substring:      %f" % (ts2 - ts1))
故事的寓意:虽然正则表达式是工具箱中一个有用的工具,但它们的效率远远不如更直接的方法


不要把别人的话当作你可以轻松测试自己的东西。

如果你有字符串的起始索引和结束索引,你可以这样做:

substring = string[s_ind:e_ind]
其中,
s_ind
是要包含在字符串中的第一个字符的索引,
e_ind
是不希望包含在字符串中的第一个字符的索引

比如说

string = "Long string of which I only want a small part"
#         012345678901234567890123456789012345678901234
#         0         1         2         3
substring = string[21:32]
print substring
打印
我只想要

你可以用和现在一样的方式找到索引


Edit:关于效率,这种类型的解决方案实际上比正则表达式解决方案的效率更高。原因是正则表达式中包含了大量开销,而您不一定需要这些开销

我鼓励你自己测试这些东西,而不是盲目地去做人们认为最有效的事情

考虑以下测试程序:

#!/bin/env python

import re
import time

def inner_regex(s):
    return re.sub(r'<[^>]*>', '', s)

def inner_substr(s):
    s_ind = s.find('>') + 1
    e_ind = s.find('<', s_ind)
    return s[s_ind:e_ind]


s = '<stuff to remove> get this stuff <stuff to remove>'

tr1 = time.time()
for i in range(100000):
    s1 = inner_regex(s)
tr2 = time.time()
print("Regex:     %f" % (tr2 - tr1))

ts1 = time.time()
for i in range(100000):
    s2 = inner_substr(s)
ts2 = time.time()
print("Substring: %f" % (ts2 - ts1))
换言之,使用正则表达式的方法,速度要慢3倍以上