gpt4 book ai didi

Python - 循环遍历嵌套字典并获取值

转载 作者:行者123 更新时间:2023-12-04 10:16:06 25 4
gpt4 key购买 nike

我在列表中有相同格式的嵌套字典。我试图遍历每个元素并找到我们拥有 summary 值的次数如 through the API并计算 through the API 的受让人数量.

[{'id': '101',
'type': 'log_file',
'summary': 'Escalated to Mark through the API',
'assignees': [{'id': 'xyz',
'type': 'user_reference',
'summary': 'Mark'}]},
{'id': '102',
'type': 'log_file',
'summary': 'Escalated to Kevin by SMS',
'assignees': [{'id': 'abc',
'type': 'user_reference',
'summary': 'Kevin'}]},
{'id': '103',
'type': 'log_file',
'summary': 'Escalated to Scott through the API',
'assignees': [{'id': 'pqr',
'type': 'user_reference',
'summary': 'Scott'}]}]

在上面的示例中,我预计次数为 2 through the API已返回 summary受让人值为 Mark and Scott因为这两个不同的人被分配了。

最佳答案

制作 Pandas 数据框:

df=pd.DataFrame(dd)

计算出现次数并将受让人姓名放在单独的列中:
df['summary'].str.contains('through the API').sum()
df['names'] = pd.Series([df.iloc[s,3][0]['summary'] for s in np.arange(0,df.shape[0])])

关于Python - 循环遍历嵌套字典并获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61048560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com