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

如何从products_dicts字典列表中提取指定uniq_id对应的Reviews字段

提取指定uniq_id产品的Reviews字段

你的代码存在几个语法和逻辑问题,修正后就能正常提取目标字段:

错误点说明

  • 遍历列表时不该加括号:products_dicts是列表,直接写for item in products_dicts即可,products_dicts()是函数调用的写法,会触发报错
  • if语句末尾缺少冒号::Python要求条件语句结尾必须加冒号,否则属于语法错误
  • 条件判断逻辑颠倒:应该判断当前遍历的产品(item)的uniq_id是否等于目标值,而非直接对比两个字符串
  • 访问Reviews字段的方式错误:要访问字典内的字段,需用item["Reviews"],products_dicts.Reviews是错误写法——列表本身没有Reviews属性

修正后的代码

target_id = "b6c0b6bea69c722939585baeac73c13d"
for item in products_dicts:
    if item["uniq_id"] == target_id:
        print(item["Reviews"])
        break  # 找到目标产品后停止遍历,提升效率

如果需要将结果保存下来而非直接打印,可使用以下写法:

target_id = "b6c0b6bea69c722939585baeac73c13d"
target_reviews = None
for item in products_dicts:
    if item["uniq_id"] == target_id:
        target_reviews = item["Reviews"]
        break

# 后续可复用target_reviews变量
if target_reviews is not None:
    print(target_reviews)
else:
    print("未找到对应uniq_id的产品")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 14:30:49