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

如何在Python中提取列表字典结构中的id字段值

正确提取id值的方法

你的变量test是一个列表,里面仅包含一个字典元素,所以不能直接对列表使用字典的键访问方式,也不能用索引1(列表索引从0开始,test[1]会超出范围),test(1)是函数调用语法,显然也不适用。

正确提取步骤:

  1. 先通过索引0获取列表中的字典元素:test[0]
  2. 再从字典中通过键'id'提取对应的值:test[0]['id']

示例代码:

test = [{'id': 28, 'testExecKey': 'ATSP-447', 'testKey': 'ATSP-446', 'status': 'TODO', 'type': 'Generic', 'defects': [], 'evidences': [], 'customFields': [], 'steps': [], 'assignee': 'Z659669', 'testEnvironments': [], 'iterations': []}]

# 提取id值
test_id = test[0]['id']
print(test_id)  # 输出:28

扩展:如果列表包含多个测试项

如果后续接口返回的列表中有多个字典元素,可以通过循环遍历提取每个元素的id:

for test_item in test:
    print(test_item['id'])

内容的提问来源于stack exchange,提问作者RoDae

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 03:20:39