How to replace a string using a dictionary containing multiple values for a key in python

后端 未结 1 1175
傲寒
傲寒 2021-01-26 03:16

I have dictionary with Word and its closest related words.

I want to replace the related words in the string with original word. Currently I am able replace words in th

1条回答
  •  情歌与酒
    2021-01-26 03:31

    I think you can replace by new dict with regex from this answer:

    d = {'Indian': 'India, Ind, ind.',
     'Restaurant': 'Hotel, Restrant, Hotpot',
     'Pub': 'Bar, Baar, Beer',
     '1888': '188, 188., 18'}
    
    d1 = {r'(?

    EDIT (Function for the above code):

    def replace_words(d, col):
        d1={r'(?

    EDIT1:

    If get errors like:

    regex error- missing ), unterminated subpattern at position 7

    is necessary escape regex values in keys:

    import re
    
    def replace_words(d, col):
        d1={r'(?

    0 讨论(0)
提交回复
热议问题