Search of Dictionary Keys python

前端 未结 6 1331
被撕碎了的回忆
被撕碎了的回忆 2021-02-20 11:46

I want to know how I could perform some kind of index on keys from a python dictionary. The dictionary holds approx. 400,000 items, so I am trying to avoid a linear search.

6条回答
  •  不思量自难忘°
    2021-02-20 12:02

    dpath can solve this for you easily.

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

    $ easy_install dpath
    >>> for (path, value) in dpath.util.search(MY_DICT, "glob/to/start/{}".format(userinput), yielded=True):
    >>> ...    # (do something with the path and value)
    

    You can pass an eglob ('path//to//something/[0-9a-z]') for advanced searching.

提交回复
热议问题