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

Python报TypeError: list indices must be integers or slices, not str如何无循环提取value值

报错原因

你得到TypeError是因为test["ticker"]的取值结果是列表类型,列表只能用整数下标或者切片取值,不能直接用字符串"value"作为索引。

无显式循环实现方案

你可以用map高阶函数结合字符串join方法实现需求,不需要手动写遍历循环:

# 方案1:用lambda配合map,无需额外导入
print('\n'.join(map(lambda item: item['value'], test['ticker'])))

# 方案2:用operator.itemgetter实现,运行效率更高
from operator import itemgetter
print('\n'.join(map(itemgetter('value'), test['ticker'])))

两种方案输出结果都符合你要求的每行一个value的格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 11:54:03