检索有序字典中每个项目的特定键的值

问题描述

对于在以下列表中很难找到有序列表中每个项目的名称值的人,将不胜感激。

OrderedDict([('12345',{'buyingprice': '600','category': 'Dairy','code': '12345','name': 'Dairy Milk','sellingprice': '800'}),('zsdfasfasf',{'buyingprice': 'test','code': 'test','name': 'test','sellingprice': 'test'})])

更清楚地说,我只想要每个项目的key(name)值

解决方法

names = [value['name'] for value in a.values()]
print(names)

输出:

['Dairy Milk','test']
,

您可以这样做:

names = [v['name'] for v in """your dict""".values()]

通过遍历字典的值并访问每个嵌套字典的“名称”键来创建名称列表。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...