Python dictionary search values for keys using regular expression

前端 未结 3 803
我寻月下人不归
我寻月下人不归 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:32

    You can solve this with dpath.

    http://github.com/akesterson/dpath-python

    dpath lets you search dictionaries with a glob syntax on the keys, and to filter the values. What you want is trivial:

    $ easy_install dpath
    >>> dpath.util.search(MY_DICT, 'seller_account*')
    

    ... That will return you a big merged dictionary of all the keys matching that glob. If you just want the paths and values:

    $ easy_install dpath
    >>> for (path, value) in dpath.util.search(MY_DICT, 'seller_account*', yielded=True):
    >>> ... # do something with the path and value
    

提交回复
热议问题