Filter a dict of dict

前端 未结 4 1167
不思量自难忘°
不思量自难忘° 2020-12-19 16:01

I new in Python and I am not sure it is a good idea to use dict of dict but here is my question. I have a dict of dict and I want to filter by the key of the inside dict:

4条回答
  •  感动是毒
    2020-12-19 16:33

    You can do with a dictionary comprehension:

    def query(data, query):
        return {key : {query : data[key][query]} 
                for key in data if query in data[key]}
    

    You have to look at each entry of the dictionary, which can cost a lot of time if you have many entries or do this a lot. A database with a index can speed this up.

提交回复
热议问题