Python 2.7 如何在python中从多个类别中搜索和替换字符串中的关键字?

Python 2.7 如何在python中从多个类别中搜索和替换字符串中的关键字?,python-2.7,replace,text-mining,Python 2.7,Replace,Text Mining,假设我有一个字符串,其内容如下: ''' Looking closely we find Pepsi and Coca-cola have been the two two biggest brands of soda in the world for the past four years. ''' 我想将单词到其类的映射表示为: classes={"NAME":["pepsi","coca-cola", "james","jill"...],"CATEGORY":["soda","food

假设我有一个字符串,其内容如下:

'''
Looking closely we find Pepsi and Coca-cola have been the two two biggest brands of soda in the world for the past four years.
'''
我想将单词到其类的映射表示为:

classes={"NAME":["pepsi","coca-cola", "james","jill"...],"CATEGORY":["soda","food","automobile"....],"NUMBER":["one","two","three","four"....]}
因此,在结尾处,我希望原始字符串为:

Looking closely we find NAME and NAME have been the two biggest brands of CATEGORY in the world for the past NUMBER years
对于一个简单的dict,例如:

rep = {"NAME": "pepsi", "CATEGORY": "soda"....}
我可以替换上面的dict的单词,但是如果每个键有一个以上的单词,我该怎么做呢

这就是我到目前为止所做的:

stringh=sentence.lower()

for i,j in rep.items():
    stringh = stringh.replace(j, i)

print stringh

遍历列表

stringh=sentence.lower()

for i,j in rep.items():
    for k in j:
        stringh = stringh.replace(k, i)

print stringh