0
假設我有一個字符串,其內容爲:如何在多個類別中搜索和替換python中的關鍵字?
'''
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
一個簡單的字典,如:
rep = {"NAME": "pepsi", "CATEGORY": "soda"....}
我可以替換上述詞典中的詞,但如果每個鍵有多個詞,我該怎麼辦?
這是我到目前爲止有:
stringh=sentence.lower()
for i,j in rep.items():
stringh = stringh.replace(j, i)
print stringh