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

如何访问Python嵌套列表内字典的start_offset和end_offset字段

报错原因

你原来的写法问题在于对annotation的结构理解有误:它是两层嵌套列表,外层列表只有1个元素(就是装所有标注字典的内层列表),遍历for i,j in annotation时,拿到的迭代对象是长度为10的字典列表,无法直接解包为i,j两个变量,因此会直接报错。

正确写法

1. 单独获取所有end_offset

end_offsets = [item['end_offset'] for item in annotation[0]]

2. 单独获取所有start_offset

start_offsets = [item['start_offset'] for item in annotation[0]]

3. 同时获取两个值,存为(起始偏移, 结束偏移)的元组列表

offset_pairs = [(item['start_offset'], item['end_offset']) for item in annotation[0]]

这里直接取annotation[0]就能拿到装所有标注字典的内层列表,直接遍历这个内层列表取对应键的值即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 03:24:02