How to search if dictionary value contains certain string with Python

后端 未结 10 1757
孤城傲影
孤城傲影 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

    def search(myDict, lookup):
        a=[]
        for key, value in myDict.items():
            for v in value:
                if lookup in v:
                     a.append(key)
        a=list(set(a))
        return a
    

    if the research involves more keys maybe you should create a list with all the keys

提交回复
热议问题