如何使用字典替换而不重复

时间:2019-06-07 23:20:53

标签: python

我有一本这样的字典:

sentences = "something @entity1 blah blah @entity123 @entity1 @entity123"
dic = {
    "@entity1":"ENN",
    "@entity123":"OTHER"
}

我想知道如何将正确的实体替换为句子。 因为有时候我会得到

"something ENN blah blah ENN23 ENN ENN23"

我的代码

a = "@entity123"
b = "@entity1"
senten = "@entity1 blah @entity123 alsdfasdka d sdfhs @entity1 @entity123"
dic = {}
dic[b] = "B"
dic[a] = "A"

for key in dic.keys():
    senten = senten.replace(key, dic[key])

senten = "B asdfasd B23 alsdfasdka d sdfhs B B23"

0 个答案:

没有答案