2013-01-18 98 views
1

仍然让我的头围绕python,我想知道这个函数是否可以提高性能或可读性?重构python:替换字符串列表中的单词列表

def multi_replace_words(sentences, words, replace_str): 
    """Replace all words in the sentences list with replace_str 
    ex. multi_replace_words(['bad a list', 'og bad', 'in bady there bad2', 'another one', 'and bad. two'], ['bad','bad2']', 'EX') 
    >> ['EX a list', 'og EX', 'in bady there EX','another one','and EX two'] 
    """ 
    docs = [] 
    for doc in sentences: 
     for replace_me in words: 
      if(replace_me in doc.encode('ascii', 'ignore')): 
       doc = re.sub('((\A|[^A-Za-z0-9_])'+replace_me+'(\Z|[^A-Za-z0-9_]))', ' ' + replace_str+' ', doc) 
     docs.append(doc) 
    return docs 

谢谢:)

+2

我会开始将ds和cls重命名为稍微更具描述性的参数名称。 –

+0

你是对的。我只是改变了变量名称,以更好地表示函数的目的,从ds,cls到句子,单词。他们只是我应用程序中数据集和类的简称(如nlp中的功能)。 – Sofia

+0

不要保留标点符号吗? –

回答

1

事情是这样的:

In [86]: def func(lis,a,b): 
    strs= "|".join("({0}{1}{2})".format(r'\b',x,r'\b[;",.]?') for x in a) 
    for x in lis: 
     yield re.sub(strs,b,x) 
    ....:   

In [87]: lis 
Out[87]: ['bad a list', 'og bad', 'in bady there bad2', 'another one', 'and bad. two'] 

In [88]: rep=['bad','bad2'] 

In [89]: st="EX" 

In [90]: list(func(lis,rep,st)) 
Out[90]: ['EX a list', 'og EX', 'in bady there EX', 'another one', 'and EX two'] 

In [91]: rep=['in','two','a'] 

In [92]: list(func(lis,rep,st)) 
Out[92]: ['bad EX list', 'og bad', 'EX bady there bad2', 'another one', 'and bad. EX'] 
0

你可以尝试使用替代()。它作用于一个字符串并将一系列字符的所有实例替换为另一个字符。来自here的示例显示了替换的行为。

#!/usr/bin/python 

str = "this is string example....wow!!! this is really string"; 
print str.replace("is", "was"); 
print str.replace("is", "was", 3); 
+1

也可以替换单词中的部分文本,就像在这个 – Sofia

+0

如果你想确保只更换完整的单词而不创建混音,就像上面的例子一样,你可以在引号中的单词之前和之后添加空格。像这样的“是”。 – JonathanV

相关问题