简体   繁体   English

字典中字典的列表理解

[英]List comprehension for dict in dict

I have the following python dictionary (a dict in a dict):我有以下 python 字典(字典中的字典):

d = {'k1': {'kk1':'v1','kk2':'v2','kk3':'v3'},'k2':{'kk1':'v4'}}

I can't get my brains to figure out the list comprehension to get a list of all values (v1, v2...).我无法让我的大脑弄清楚列表理解以获取所有值(v1,v2 ...)的列表。 If you can give my an example with a lambda also, that you be nice.如果你也可以给我一个 lambda 的例子,那你就很好了。

The goal is to have values_lst = ['v1','v2','v3','v4']目标是让 values_lst = ['v1','v2','v3','v4']

Thanks谢谢

Combine two loops to "flatten" the dict of dicts.结合两个循环来“展平”dicts 的dict。 Loop over the values of d first, and then loop over the values of the values of d .首先循环d的值,然后循环d的值。 The syntax might be a bit hard to grasp at first:语法起初可能有点难以掌握:

values_lst = [v for x in d.values() for v in x.values()]

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

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