我试图将句子中的特定单词替换为不同的名称,每个单词都会有一个新名称。例如:
my_words = {[ 'a','b'],['c','d','e','f'], ['l','m','n']}
my_sentences = {' w0 w1 a w2 w3 b w4' , ' w0 w1 w2 c w3 d w4 e f' , 'w0 w1 w2 l m w3 w4 n w5']
我想替换(a,'first_word')
并替换第一句中的(b ,' second_word')
。另外,我想要替换(c,'first_word')
和(d, 'second_word')
,并且列表中的其余字词(e,f)将替换为第二句中的'other_word'
。
我编写了一个代码,用于将所有特定单词替换为'first_word'
。请参阅以下代码:
def replace_all(sentences=[], words = []):
text = []
A_regex = re.compile('|'.join(map(re.escape, words)))
for t in sentences:
t = A_regex.sub("first_word", t)
text.append(t)
return text
我尝试了另一个代码:
for t in sentences:
for w in words:
for j in range (len(w)):
t = t.replace(w[j][0],'FIRST_word')
t = t.replace(w[j][1],'SECOND_word')
if j == -1:
break
else:
t = t.replace(w[j][2:-1],'OTHER_words')
break
但它不起作用,
感谢您的帮助或任何提示。
答案 0 :(得分:0)
按照您的方法,您可以按以下方式修复它:
# You need to add spaces before and after each letter to avoid replacing letters in words.
my_words = [[' a ', ' b '], [' c ', ' d ', ' e ', ' f '], [' l ', ' m ', ' n ']]
my_sentences = ['w0 w1 a w2 w3 b w4', ' w0 w1 w2 c w3 d w4 e f', 'w0 w1 w2 l m w3 w4 n w5']
for i, c in enumerate(my_words):
for j, word in enumerate(c):
if j == 0:
my_sentences[i] = my_sentences[i].replace(word, ' first_word ')
elif j == 1:
my_sentences[i] = my_sentences[i].replace(word, ' second_word ')
else:
my_sentences[i] = my_sentences[i].replace(word, ' other_word ')
print my_sentences
输出:
['w0 w1 first_word w2 w3 second_word w4', ' w0 w1 w2 first_word w3 second_word w4 other_word f', 'w0 w1 w2 first_word second_word w3 w4 other_word w5']
但是,我强烈建议您使用dictionary
来提高效率。