How to search if dictionary value contains certain string with Python

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

    >>> myDict
    {'lastName': ['Stone', 'Lee'], 'age': ['12'], 'firstName': ['Alan', 'Mary-Ann'],
     'address': ['34 Main Street, 212 First Avenue']}
    
    >>> Set = set()
    
    >>> not ['' for Key, Values in myDict.items() for Value in Values if 'Mary' in Value and Set.add(Key)] and list(Set)
    ['firstName']
    

提交回复
热议问题