根据起始索引和结束索引删除字符串

时间:2012-04-10 17:07:22

标签: 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 = "<" 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

实施此

的更好方法是什么?

4 个答案:

答案 0 :(得分:2)

使用正则表达式:

>>> s = "< stuff to remove> get this stuff <stuff to remove>"
>>> import re
>>> re.sub(r'<[^<>]*>', '', s)
' get this stuff '

表达式<[^<>]*>匹配以<开头的字符串,以>结尾,中间不包含<>。然后sub命令将匹配替换为空字符串,从而删除它。

然后,您可以在结果上调用.strip()以删除前导和尾随空格。

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

答案 1 :(得分:2)

正则表达式是一种简单的方法(虽然不一定更快,如jedwards的答案所示):

import re
s = '< stuff to remove> get this stuff <stuff to remove>'
s = re.sub(r'<[^>]*>', '', s)

s之后是字符串' get this stuff '

答案 2 :(得分:2)

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

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

您要执行的操作类似于x[:strt_idx] + x[end_idx:]。 (如果省略第一个参数,则表示“从头开始”,如果省略第二个,则表示“继续到最后”。)

答案 3 :(得分:0)

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

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

打印I only want

你可以用与现在相同的方式找到指数。


编辑:关于效率,此类解决方案实际上比正则表达式解决方案 更高效 。原因是你不一定需要正则表达式中涉及很多开销。

我鼓励你为自己测试这些东西,而不是盲目地继续人们所说的最有效率。

考虑以下测试程序:

#!/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))

输出是:

Regex:     0.511443
Substring: 0.148062

换句话说,使用正则表达式方法比原始的纠正方法要快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))

返回:

Regex:          0.512799  # >3 times slower
Regex Compiled: 0.297863  # ~2 times slower
Substring:      0.144910

故事的道德: 虽然正则表达式是工具箱中的一个有用的工具,但它们在可用时根本不如更直接的方式有效。< / p>

不要用人的话说出你可以轻易测试的事情。

相关问题