本文介绍了字典列表列表中的键列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个列表的字典列表。我很确定Python有一个很好的和简短的方式(不用写2个循环)来检索与该关键字相关联的值。 (每个字典都有一个键)。
我该如何做?
编辑:有一个示例(JSON表示)输入
[
[
{
lat:45.1845931,
lgt :5.7316984,
name:Chavant,
sens:,
line:[],
stationID:
$,
{
lat:45.1845898,
lgt:5.731746,
name:Chavant,
sens ,
line:[],
stationID:
}
],
[
{
:45.1868233,
lgt:5.7565727,
name:Neyrpic - Belledonne,
sens:,
line:[],
stationID:
},
{
lat:45.1867322,
lgt:5.7568569,
name:Neyrpic - Belledonne,
sens:,
line:[],
stationID:
}
]
]
作为输出,我想要一个名单列表。
PS:ODBL下的数据。
解决方案
如果您需要全部平面列表中的名称:
response =#你的大列表
[d.get('name')for lst如果要使用内部列表获取结果,则为
[[d.get('name')for d in lst] for lst in response]
Suppose I have a list of a list of dictionaries.
I'm pretty sure Python has a nice and short way (without writing 2 for loops) to retrieve the value associated with the key. (Every dictionary has a key).
How can I do this?
Edit : There is a sample (JSON representation) of the input
[
[
{
"lat": 45.1845931,
"lgt": 5.7316984,
"name": "Chavant",
"sens": "",
"line" : [],
"stationID" : ""
},
{
"lat": 45.1845898,
"lgt": 5.731746,
"name": "Chavant",
"sens": "",
"line" : [],
"stationID" : ""
}
],
[
{
"lat": 45.1868233,
"lgt": 5.7565727,
"name": "Neyrpic - Belledonne",
"sens": "",
"line" : [],
"stationID" : ""
},
{
"lat": 45.1867322,
"lgt": 5.7568569,
"name": "Neyrpic - Belledonne",
"sens": "",
"line" : [],
"stationID" : ""
}
]
]
As output I'd like to have a list of names.
PS: Data under ODBL.
解决方案
If you need all names in flat list:
response = # your big list
[d.get('name') for lst in response for d in lst]
if you want to get result with inner list:
[[d.get('name') for d in lst] for lst in response]
这篇关于字典列表列表中的键列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!