Python:按索引删除子字符串

可能

我有以下相当简单的代码段:

def delete_substring_blocks(s, blocks):                                                                             
  '''                                                                                                                   
      s: original input string                                                                                   
      blocks: list of indices (start, end) to be deleted                                                                

      return string `out` where blocks are deleted from s                                                      
  '''                                                                                                                   
  out = ''                                                                                                              
  p = 0                                                                                                                 
  for start, end in blocks:                                                                                             
      out += s[p:start]                                                                                               
      p = end                                                                                                           
  out += s[p:]                                                                                                        
  return out

这个函数接受一个字符串ss[start:end]从中删除全部s,在(start, end)列表中给出索引对blocks

是否有内置函数可以执行相同的操作?


更新:我的代码中有一个假设:

  1. 块按第一个索引升序排序(list.sort()就地完成)

至于块是否可以重叠,在我的用例中,我确保在调用函数之前它们不会重叠。但是为了好玩,我们也可以假设他们这样做。

海武

我的方法转变blocks为一组称为索引的索引exclude之后,遍历字符串并排除索引位于exclude集合中的那些字符我使用set而不是list,因为它很好地处理了重复项(在重叠范围的情况下)。

建立exclude集合

给定一个无序的,可能重叠的范围列表:

blocks = [(5, 7), (2, 4), (6, 10)]

我想将其转换为:

exclude = {2, 3, 5, 6, 7, 8, 9}

怎么样:

exclude = set()
for block in blocks:
    exclude.update(range(*block))

放在一起

这是我的代码,最后是一个小例子。请注意,我选择重命名该函数是因为该函数具有足够的通用性,可以处理字符串,列表,元组和其他可迭代对象,而不仅仅是字符串。另外,由于该函数返回一个列表,因此在处理字符串时,我们需要将字符列表重新连接在一起。

def delete_blocks(iterable, blocks):                                                                             
    exclude = set()
    for block in blocks:
        exclude.update(range(*block))
    return [cell for i, cell in enumerate(iterable) if i not in exclude]

# Try it out
test_string = '0123456789abc'
blocks = [(5, 7), (2, 4), (6, 10)]
result = ''.join(delete_blocks(test_string, blocks))

print('Before: {!r}'.format(test_string))
print('Blocks:', blocks)
print('After: {!r}'.format(result))

更新:实施 delete_substring_blocks

为了真正回答Mai的问题,我delete_substring_blocks使用了delete_blocks

def delete_substring_blocks(s, blocks):
    return ''.join(delete_blocks(s, blocks))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

NSIS子字符串按索引

Python:按索引在字符串中查找子字符串

Python按索引从字符串中删除字符的最佳方法

熊猫索引 - 按数字子字符串对字符串索引进行排序

按名称包含子字符串的索引选择列

按多个索引位置过滤子字符串

按索引从字符串[]中删除行

使用Python删除子字符串

使用Python删除子字符串

Python删除子字符串

xslt按子字符串重复数据删除

如何从特定索引的字符串中快速删除子字符串到某个长度

如何从Rust字符串或&str类型的索引中删除子字符串?

按预定义索引拆分 python 字符串

Python:在字符串中查找子字符串并返回该子字符串的索引

按索引删除数组项(不是数字索引而是字符串)

通过索引“同时”插入多个python子字符串

从指定索引处的字符串列表中删除不需要的子字符串

按列表中的子字符串排序 - Python

有没有办法从R中的字符串中按索引删除字符?

按索引置換字符串

按索引更改字符串

Python获取字符串中所有子字符串出现的索引范围

Python:在字符串中查找子字符串,但返回 True 或 False 而不是索引位置

在Python中按空格分隔字符串-保留带引号的子字符串

在python中按子字符串数值对字符串排序

在Python中按空格将字符串拆分为最大长度的子字符串

检测子字符串,然后在子字符串之后删除字符串的其余部分,包括python中的子字符串本身

使用python删除pandas DataFrame中的子字符串