简体   繁体   English

有没有办法选择词典列表中存在的词典的特定键值

[英]Is there a way to select particular key value of dictionary present in a list of dictionaries

I am presently using a for loop to print all the required key value pairs of a dictionary. 我目前正在使用for循环来打印字典中所有必需的键值对。 However is there a simpler way to select the required key value pair? 但是,有一种更简单的方法来选择所需的键值对吗?

        for i in (out['elements']):
            out = (i['insights'][0]['details']['socialInfo'])
            out_temp.append(out)

The content of out is actually a JSON with list of dictionaries and each dictionary contains a list of dictionaries. out的内容实际上是带有字典列表的JSON,每个字典都包含一个字典列表。

I cannot see an unequivocally simpler way to access the data you require. 我看不到访问所需数据的简单明了的方法。 However, you can apply your logic more efficiently via a list comprehension: 但是,您可以通过列表理解来更有效地应用逻辑:

out_temp = [i['insights'][0]['details']['socialInfo'] for i in out['elements']]

Whether or not this is also simpler is open to debate. 这是否更简单还有待商debate。

You can use map to generate the new list as well. 您也可以使用map生成新列表。 But I think what you are doing is fine, it is much easier to read than the alternatives. 但是我认为您正在做的事情很好,比其他选择更容易阅读。

out_temp = list(map(lambda x: x['insights'][0]['details']['socialInfo'], out['elements']))

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

相关问题 根据该字典中某个键的特定值过滤字典列表 - filter list of dictionaries based on a particular value of a key in that dictionary 有没有办法在字典列表中的下一个字典中获取相同键的下一个值? - Is there a way to get next value for the same key in the next dictionary in the list of dictionaries? 有没有更好的方法来动态地从字典列表中创建具有键和值的字典? - Is there a better way to create a dictionary with key and value from a list of dictionaries dynamically? 字典列表作为字典列表键的值 - List of Dictionaries as a value for key of list of dictionary 检查字典列表中是否存在键,如果为真,则返回列表中的字典索引 - Check if key is present in list of dictionaries and if true return dictionary index in list 根据特定键值组合字典列表 - Combine list of dictionaries based on particular key value 如何将字典列表附加到字典中的键值? - How to append a list of dictionaries to the value of a key in a dictionary? 如何使用键比较在字典列表中查找特定字典 - How to find a particular dictionary in a list of dictionaries using key comparisons 遍历字典列表以检查每个字典是否包含特定键 - looping through a list of dictionaries to check if each dictionary contains a particular key 浏览字典列表,仅选择存在键的行 - Go through list of dictionaries and select only rows where a key is present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM