简体   繁体   中英

Unpacking a list of dictionaries based on key matches in Python

I am looking for a way to unpack a list of dictionaries with a certain key ID. I have seen a lot of examples based on a key value, but nothing on just a key match. Say I have a dictionary in the following format returned from a function...

data = [{'Lev1': u'82', 'Marker': u'16', 'TYPE': u'139', 'Location': u'A'},
        {'Lev2': u'652', 'Marker': u'1', 'TYPE': u'140', 'Location': u'C'},
        {'Lev3': u'452', 'Marker': u'188', 'TYPE': u'141', 'Location': u'B'}]

My current attempt is shown below, but I am getting >> TypeError: list indices must be integers, not str

for item in data:
    parts[data['TYPE']].update(data)

The parts reference above is a dictionary of parts numbers. I am looking to drop each of the following list entries (for example, 'Lev1': u'82', 'Marker': u'16', 'TYPE': u'139', 'Location': u'A' ) into the main 'parts' dictionary based on a TYPE (there should already be a TYPE match in the parts dictionary).

My method works for a single returned dictionary entry...

parts[data['TYPE']].update(data)

...but just not with a list of dictionaries.

I am looking to end up with a format along the lines of...

parts{
    125:
    ...
    ...
    ...
    139:{
        'Lev1': u'82', 
        'Marker': u'16', 
        'TYPE': u'139', 
        'Location': u'A'
        plus other previously gathered data
        }

    140:{
        'Lev2': u'652', 
        'Marker': u'1', 
        'TYPE': u'140', 
        'Location': u'C'
        plus other previously gathered data
        }

    141:{
        'Lev3': u'452', 
        'Marker': u'188', 
        'TYPE': u'141', 
        'Location': u'B'
        plus other previously gathered data
        }
    142:etc
    ...
}

You can update with item instead of data

for item in data:
    parts[item['TYPE']].update(item)

Result

>>> parts
{'141': {'Lev3': '452', 'TYPE': '141', 'Marker': '188', 'Location': 'B'},
 '140': {'Lev2': '652', 'TYPE': '140', 'Marker': '1', 'Location': 'C'},
 '139': {'Lev1': '82', 'Marker': '16', 'Location': 'A', 'TYPE': '139'}}

I think here is what you want, using item['TYPE'] as a key match. And you should use item instead of data .

for item in data:
    if isinstance(parts, dict):
        # when there is a dictionary in parts with key=item['TYPE'], update it
        if parts.get(item['TYPE']):
            parts[item['TYPE']].update(item)
        # when there is nothing in parts with key=item['TYPE'], add item to it
        else:
            parts[item['TYPE']]=item

'TYPE' is not the index of data . It is the key of dictionary item in list data . You can do it as following:

for item in data:
    if item['TYPE'] in parts:
        parts[item['TYPE']].update(item)
    else:
        parts[item['TYPE']]=item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM