[英]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.