How to search if dictionary value contains certain string with Python

后端 未结 10 1754
孤城傲影
孤城傲影 2020-12-02 17:58

I have a dictionary with key-value pair. My value contains strings. How can I search if a specific string exists in the dictionary and return the key that correspond to the

10条回答
  •  悲哀的现实
    2020-12-02 18:28

    import re
    for i in range(len(myDict.values())):
         for j in range(len(myDict.values()[i])):
                 match=re.search(r'Mary', myDict.values()[i][j])
                 if match:
                         print match.group() #Mary
                         print myDict.keys()[i] #firstName
                         print myDict.values()[i][j] #Mary-Ann
    

提交回复
热议问题