如何编写函数根据author_id从JSON专家列表获取对应全部数据?
现有如下专家JSON数据:
result={ "experts":[ { "thumbnail": "https://scholar.googleusercontent.com/citations?view_op=small_photo&user=JicYPdAAAAAJ&citpid=2", "name": "Geoffrey Hinton", "link": "https://scholar.google.com/citations?hl=en&user=JicYPdAAAAAJ", "author_id": "JicYPdAAAAAJ", "email": "Verified email at cs.toronto.edu", "affiliations": "Emeritus Prof. Comp Sci, U.Toronto & Engineering Fellow, Google", "cited_by": 638900, "interests": [ { "title": "machine learning", "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Amachine_learning", "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:machine_learning" }, { "title": "psychology", "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Apsychology", "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:psychology" }, { "title": "artificial intelligence", "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Aartificial_intelligence", "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:artificial_intelligence" }, { "title": "cognitive science", "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Acognitive_science", "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:cognitive_science" }, { "title": "computer science", "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Acomputer_science", "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:computer_science" } ] }, { "thumbnail": "https://scholar.googleusercontent.com/citations?view_op=small_photo&user=kukA0LcAAAAJ&citpid=3", "name": "Yoshua Bengio", "link": "https://scholar.google.com/citations?hl=en&user=kukA0LcAAAAJ", "author_id": "kukA0LcAAAAJ", "email": "Verified email at umontreal.ca", "affiliations": "Professor of computer science, University of Montreal, Mila, IVADO, CIFAR", "cited_by": 605714, "interests": [ { "title": "Machine learning", "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Amachine_learning", "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:machine_learning" }, { "title": "deep learning", "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Adeep_learning", "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:deep_learning" }, { "title": "artificial intelligence", "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Aartificial_intelligence", "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:artificial_intelligence" } ] } ] }需求:编写函数,输入指定
author_id,返回对应专家的全部关联数据(包括thumbnail、name、email等)。
解决方案
通过遍历experts数组,匹配每个专家对象的author_id字段即可实现。以下是Python实现代码:
def get_expert_by_id(author_id, data): # 遍历专家列表匹配目标ID for expert in data["experts"]: if expert["author_id"] == author_id: return expert # 未找到匹配项时返回None return None
使用示例
# 调用函数查询指定ID的专家 target_expert = get_expert_by_id("JicYPdAAAAAJ", result) if target_expert: print("匹配到的专家信息:") print(target_expert) else: print("未找到对应ID的专家")
说明
- 函数接收两个参数:要查询的
author_id,以及包含专家数据的字典data。 - 遍历
data["experts"]中的每个元素,对比author_id字段,匹配成功则返回该专家的完整数据。 - 若遍历结束未找到匹配项,返回
None,可根据需求修改为返回自定义提示信息。
内容的提问来源于stack exchange,提问作者SIM777
相关产品推荐
相关产品推荐

