You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写函数根据author_id从JSON专家列表获取对应全部数据?

根据author_id获取专家信息的解决方案

现有如下专家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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:45:02