替换功能是多次替换单词和组合单词。我怎样才能解决这个问题?

2024-06-08 22:14:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个要在excel中替换的单词字典,并使用它在Python中一次替换许多单词。然而,单词被多次替换,甚至由两个单词组成新词。你知道吗

我一直在使用replace()函数

#  dictionary
all_synonyms = alias_all['Synonyms']
all_standard = alias_all['Standard']
dictionary = dict(zip(all_synonyms,all_standard))
#print(dictionary)


df= df.replace(dictionary, regex=True)
pd.set_option('display.max_colwidth', -1)
print(df.loc[97])

在我的文本中,我想要

mag plug 

替换

magnetic plug

(正如我在字典里所定义的)

然而

mag plug

正在替换为

magnetic plug magnetic plugnetic plug

Tags: dfdictionary字典aliasall单词excelreplace