Python dictionary search values for keys using regular expression

前端 未结 3 751
我寻月下人不归
我寻月下人不归 2021-01-31 16:15

I am trying to implement to search for a value in Python dictionary for specific key values (using regular expression as a key).

Example:

I have a Python dictio

3条回答
  •  孤独总比滥情好
    2021-01-31 16:42

    def search(dictionary, substr):
        result = []
        for key in dictionary:
            if substr in key:
                result.append((key, dictionary[key]))   
        return result
    
    >>> my_dict={'account_0':123445,'seller_account':454545,'seller_account_0':454676, 'seller_account_number':3433343}
    >>> search(my_dict, 'seller_account')
    [('seller_account_number', 3433343), ('seller_account_0', 454676), ('seller_account', 454545)]
    

提交回复
热议问题