[英]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. 我正在寻找一种解密具有特定密钥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 我目前的尝试如下所示,但我得到>> TypeError:list indices必须是整数,而不是str
for item in data:
parts[data['TYPE']].update(data)
The parts
reference above is a dictionary of parts numbers. 上面的
parts
参考是零件编号的字典。 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). 我希望删除以下每个列表条目(例如,
'Lev1': u'82', 'Marker': u'16', 'TYPE': u'139', 'Location': u'A'
)基于TYPE
进入主'部分'字典( parts
字典中应该已经存在TYPE匹配)。
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
您可以使用
item
而不是data
进行update
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. 我认为这是你想要的,使用
item['TYPE']
作为关键匹配。 And you should use item
instead of data
. 你应该使用
item
而不是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
. 'TYPE'
不是data
索引。 It is the key of dictionary item
in list data
. 它是列表
data
中字典item
的关键。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.