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

如何遍历嵌套字典?以提取price和market_hash_name字段值为例

遍历嵌套字典提取指定字段值

你可以通过以下方式遍历嵌套字典,提取所有price和market_hash_name字段的值:

方式一:直接遍历打印结果

假设你的字典数据存储在data变量中,使用Python代码实现:

data = {
   "success": True,
   "time": 1660441201,
   "currency": "RUB",
   "items": {
      "186150629_143865972": {
         "price": "300.00",
         "buy_order": 220,
         "avg_price": "279.405789",
         "popularity_7d": "19",
         "market_hash_name": "CS:GO Case Key",
         "ru_name": "Ключ от кейса CS:GO",
         "ru_rarity": "базового класса",
         "ru_quality": "",
         "text_color": "D2D2D2",
         "bg_color": ""
      },
      "186150630_143865972": {
         "price": "993.06",
         "buy_order": 200.02,
         "avg_price": "573.320000",
         "popularity_7d": "1",
         "market_hash_name": "eSports Key",
         "ru_name": "Ключ eSports",
         "ru_rarity": "базового класса",
         "ru_quality": "",
         "text_color": "D2D2D2",
         "bg_color": ""
      }
   }
}

# 遍历items下的所有商品详情
for item_id, item_details in data["items"].items():
    print(f"商品名称: {item_details['market_hash_name']}, 价格: {item_details['price']}")

运行后会输出:

商品名称: CS:GO Case Key, 价格: 300.00
商品名称: eSports Key, 价格: 993.06

方式二:将结果存储为列表

如果需要把提取的数据保存下来后续使用,可以将结果存入列表:

result = []
# 只遍历商品详情,不需要唯一标识的话用values()
for item_details in data["items"].values():
    result.append({
        "商品名称": item_details["market_hash_name"],
        "价格": item_details["price"]
    })

# 查看存储的结果
print(result)

运行后会得到一个字典列表:

[{'商品名称': 'CS:GO Case Key', '价格': '300.00'}, {'商品名称': 'eSports Key', '价格': '993.06'}]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 12:15:42