Python中的字符串覆盖优化 [英] String coverage optimization in Python

查看:122
本文介绍了Python中的字符串覆盖优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个初始字符串。

 'bananaappleorangestrawberryapplepear'

还有一个带字符串的元组:

 ('apple ','plepe','leoran','lemon')

我想要一个函数,我得到这个:

 'bananaxxxxxxxxxgestrawberryxxxxxxxar'



我知道如何通过在每个单词的初始字符串中找到单词,然后在所有初始字符串中用逐字替换单词来逐字地循环。

但这不是非常有效和丑陋。我怀疑应该有一些更优雅的方式,以功能的方式,使用itertools或其他方法。如果您知道一个可以有效执行此操作的Python库,请告诉我们。



更新:Justin Peel指出我没有在我最初的问题中描述。如果一个单词是'aaa',并且'aaaaaa'在初始字符串中,那么输出应该看起来像'xxxxxx'。

解决方案

  import re 

words =('apple','plepe','leoran','lemon')
s ='bananaappleorangestrawberryapplepear'

x = set()

for w in words:
for m in re.finditer(w,s):
i = m.start()
for j in range(i,i + len(w)):
x.add(j)

result =''.join(('x'if i in x (len(s)))
打印结果

产生:

  bananaxxxxxxxxxgestrawberryxxxxxxxar 


I have this initial string.

'bananaappleorangestrawberryapplepear'

And also have a tuple with strings:

('apple', 'plepe', 'leoran', 'lemon')

I want a function so that from the initial string and the tuple with strings I obtain this:

'bananaxxxxxxxxxgestrawberryxxxxxxxar'

I know how to do it imperatively by finding the word in the initial string for every word and then loop character by character in all initial string with replaced words.

But it's not very efficient and ugly. I suspect there should be some way of doing this more elegantly, in a functional way, with itertools or something. If you know a Python library that can do this efficiently please let me know.

UPDATE: Justin Peel pointed out a case I didn't describe in my initial question. If a word is 'aaa' and 'aaaaaa' is in the initial string, the output should look like 'xxxxxx'.

解决方案

import re

words = ('apple', 'plepe', 'leoran', 'lemon')
s = 'bananaappleorangestrawberryapplepear'

x = set()

for w in words:
    for m in re.finditer(w, s):
        i = m.start()
        for j in range(i, i+len(w)):
            x.add(j)

result = ''.join(('x' if i in x else s[i]) for i in range(len(s)))
print result

produces:

bananaxxxxxxxxxgestrawberryxxxxxxxar

这篇关于Python中的字符串覆盖优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆