Python-通过嵌套字典循环并获取值
我在列表中有相同格式的嵌套词典。我试图遍历每个元素,并通过API查找我们拥有Python-通过嵌套字典循环并获取值,python,list,dictionary,Python,List,Dictionary,我在列表中有相同格式的嵌套词典。我试图遍历每个元素,并通过API查找我们拥有摘要值的时间,以及通过API计算的受让人数量 [{'id': '101', 'type': 'log_file', 'summary': 'Escalated to Mark through the API', 'assignees': [{'id': 'xyz', 'type': 'user_reference', 'summary': 'Mark'}]}, {'id': '102', 'type':
摘要值的时间,以及通过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'}]}]
在上面的示例中,我预计通过API返回summary
的次数为2次,而受让人值为Mark和Scott
,因为这两个不同的人已经被分配了。您可以循环浏览列表。以下是我的例子:
data=[{'id':'101','type':'log_file','summary':'通过API升级到Mark','assignees':[{'id':'xyz','type':'user_reference','summary':'Mark'}},{'id':'log_file','summary':'通过短信升级到Kevin','assignees':[{'id':'abc','type':'user_reference','summary':'Kevin'},{'id':'103','type':'log_file','summary':'通过API升级到Scott','assignees':[{'id':'pqr','type':'user_reference','summary':'Scott'}]
人员=[sum[“assignees”][0][“summary”]表示数据中的sum,如果sum[“summary”]“through the API”]。#这将返回[“Mark”,“Scott”]
被指派者的数量=len(人)#这将返回2
制作熊猫数据帧:
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])])
好的,谢谢,我不想把它转换成数据帧