Filter a dict of dict

前端 未结 4 1124
不思量自难忘°
不思量自难忘° 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:31
    field = 'id1'
    dict( (k,{field: d[field]}) for k,d in a.items() if field in d)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-19 16:38

    According to the precision you gave to Duncan, here is another filtering on a list using dictionary comprehension:

    >>> my_list = ['id1', 'id2']
    >>> {k1 : {k2: v2 for (k2, v2) in a[k1].iteritems() if k2 in my_list} for k1 in a}
    {'key3': {'id1': [4, 5, 6]}, 'key2': {}, 'key1': {'id2': [0, 1, 2], 'id1': [0, 1, 2]}}
    

    EDIT: you can also remove empty values with another dict compreehension, but that "begins" to be difficult to read... :-)

    >>> {k3: v3 for k3, v3 in {k1 : {k2: v2 for (k2, v2) in a[k1].iteritems() if k2 in my_list} for k1 in a}.iteritems() if v3}
    {'key3': {'id1': [4, 5, 6]}, 'key1': {'id2': [0, 1, 2], 'id1': [0, 1, 2]}}
    
    0 讨论(0)
  • 2020-12-19 16:41

    Try this:

    >>> { k: v['id1'] for k,v in a.items() if 'id1' in v }
    {'key3': [4, 5, 6], 'key1': [0, 1, 2]}
    

    For Python 2.x you might prefer to use iteritems() instead of items() and you'll still need a pretty recent python (2.7 I think) for a dictionary comprehension: for older pythons use:

    dict((k, v['id1']) for k,v in a.iteritems() if 'id1' in v )
    

    If you want to extract multiple values then I think you are best to just write the loops out in full:

    def query(data, wanted):
        result = {}
        for k, v in data.items():
            v2 = { k2:v[k2] for k2 in wanted if k2 in v }
            if v2:
                result[k] = v2
        return result
    

    giving:

    >>> query(a, ('id1', 'id2'))
    {'key3': {'id1': [4, 5, 6]}, 'key1': {'id2': [0, 1, 2], 'id1': [0, 1, 2]}}
    
    0 讨论(0)
提交回复
热议问题