简体   繁体   English

如何仅从字典列表中获取值,然后在Python中再次将它们放在列表中?

[英]How to get only values from a list of dictionary and put them in a list again in Python?

I am running a query in the db and getting the output in a list of dict format. 我正在数据库中运行查询,并以dict格式列表获取输出。 Each list of dict is associated with a id column. 每个字典列表都与一个id列相关联。 Example: 例:

[{'v_key':50},{'v_key':60}] # say here the id is 1
[{'r_key':10}] # here the id is 2 (the output go on like this and key in dict may not be same)

so to loop thru the output of the query i need something like below: 因此,要遍历查询的输出,我需要如下所示的内容:

[50,60] # for id 1.(The values to be in a list and this will go on for all the id's)

[v for dct in lst for v in dct.values()] . [v for dct in lst for v in dct.values()] This will have undefined order if dicts have more than one value. 如果字典具有多个值,则将具有不确定的顺序。

如果query_result = [{'v_key':50},{'v_key':60}] ,则:

[d['v_key'] for d in query_result]  # [50, 60]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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