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

如何从Python字典中提取满足多键值匹配条件的price值

Python提取指定条件订单价格实现方案

核心逻辑

接口返回的所有订单数据存储在根字典的data字段中,类型为列表。遍历该列表,对每个条目同时校验三个筛选规则,匹配成功即可取出对应的price字段值:

  • direction 字段值为 'BUY'
  • trading_pair_id 字段值为 48
  • status 字段值为 'FILLED'

代码实现

假设接口返回的字典数据已存储在变量api_resp中,基础遍历写法如下:

matched_prices = []
for order in api_resp["data"]:
    if (
        order["direction"] == "BUY"
        and order["trading_pair_id"] == 48
        and order["status"] == "FILLED"
    ):
        matched_prices.append(order["price"])

如果需要更简洁的写法,可以用列表推导式:

matched_prices = [
    order["price"]
    for order in api_resp["data"]
    if order["direction"] == "BUY"
    and order["trading_pair_id"] == 48
    and order["status"] == "FILLED"
]

样例数据运行结果

用提供的样例数据运行上述代码,得到的匹配价格列表为:

['0.1200', '0.1414', '0.1302', '0.1302']

如果后续需要做数值计算,可以在取值时直接将字符串格式的价格转为浮点数,把append(order["price"])替换为append(float(order["price"]))即可,转换后结果为:

[0.12, 0.1414, 0.1302, 0.1302]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 17:30:46