[英]Fetching values from dictionaries nested in a list?
I'm pulling data out from the facebook API, what it returns is essentially a nested JSON looking file. 我正在从facebook API中提取数据,它返回的实际上是一个嵌套的JSON查找文件。
I've piped the data into the a pandas dataframe and one of the columns has returned a series of dictionaries nested in a list. 我已经将数据通过管道传递到pandas数据框,并且其中一列返回了嵌套在列表中的一系列字典。 (see examples in the code below). (请参见下面的代码中的示例)。
How on Earth do I extract the values from each variable (ie a, b, c) when the action_type = landing_page_view? 当action_type = landing_page_view时,如何从每个变量(即a,b,c)中提取值?
ie in variable a, how do I extract the value when searching for specific action types? 即在变量a中,当搜索特定操作类型时如何提取值?
ie return 44, 96 and 116 when I want to search the variables for 'landing_page_view'? 即当我想搜索变量“ landing_page_view”时返回44、96和116?
I've tried various for loops and things of that nature but I am not having any luck. 我已经尝试过各种for循环和类似性质的东西,但是我没有任何运气。 I'm at a loss... 我不知所措...
a = [{'action_type': 'landing_page_view', 'value': '44'}, {'action_type': 'link_click', 'value': '102'}, {'action_type': 'post_reaction', 'value': '5'}, {'action_type': 'post_engagement', 'value': '107'}, {'action_type': 'page_engagement', 'value': '107'}]
b = [{'action_type': 'comment', 'value': '1'}, {'action_type': 'landing_page_view', 'value': '96'}, {'action_type': 'link_click', 'value': '285'}, {'action_type': 'post_reaction', 'value': '25'}, {'action_type': 'post_engagement', 'value': '311'}, {'action_type': 'page_engagement', 'value': '311'}]
c = [{'action_type': 'post_reaction', 'value': '11'}, {'action_type': 'landing_page_view', 'value': '116'}, {'action_type': 'link_click', 'value': '319'}, {'action_type': 'post_engagement', 'value': '330'}, {'action_type': 'page_engagement', 'value': '330'}]
for item in a:
if item['action_type'] == 'landing_page_view':
print(item['value'])
Repeat for b
and c
. 重复b
和c
。
You can access it like you would normally but with the list and position in the begining 您可以像平常一样访问它,但要在开头加上列表和位置
example: 例:
lists = [{'hello':'world', 'bye':'world'}]
print (lists[0]['hello'])
lists[0]['hello'] = "user"
print (lists[0]['hello'])
output: 输出:
world
user
一线解决方案:
[item['value'] for item in a if item['action_type'] == 'landing_page_view']
I feel like Jonathan's approach is best for this! 我觉得乔纳森(Jonathan)的方法最适合此!
[item['value'] for item in a if item['action_type'] == 'landing_page_view'] if项目['action_type'] =='landing_page_view']中项目的[item ['value']
Here's another way of doing it for those who don't like list comprehensions so much: 对于那些不太喜欢列表理解的人,这是另一种方法:
from cherrypicker import CherryPicker
picker = CherryPicker(a)
picker(action_type='landing_page_view')['value'].get()
Install cherrypicker
with pip install --user cherrypicker
. 安装cherrypicker
与pip install --user cherrypicker
。 Read about more advanced usage in the docs: https://cherrypicker.readthedocs.io . 在文档中了解更多高级用法: https : //cherrypicker.readthedocs.io 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.