如何用Python查找JSON数据中的最小platinum值
问题描述
现有如下JSON数据片段:
{ "payload": { "orders": [ { "quantity": 1, "platinum": 4, "visible": true, "order_type": "sell", "user": { "reputation": 5, "region": "en", "last_seen": "2022-11-17T08:15:43.360+00:00", "ingame_name": "Noxxat", "id": "5b50d73859d885026b523cd1", "avatar": null, "status": "offline" }, "platform": "pc", "region": "en", "creation_date": "2020-09-04T15:30:41.000+00:00", "last_update": "2021-11-19T09:41:43.000+00:00", "id": "5f525da1c98cd000d7513813" }, { "order_type": "sell", "visible": true, "quantity": 2, "platinum": 6, "user": { "reputation": 3, "region": "en", "last_seen": "2022-11-18T14:22:53.023+00:00", "ingame_name": "Dhatman", "id": "5b79921649262103f74b6585", "avatar": null, "status": "offline" }, "platform": "pc", "region": "en", "creation_date": "2020-11-06T10:32:32.000+00:00", "last_update": "2022-10-11T16:51:55.000+00:00", "id": "5fa526406ff3660486ef556c" }, { "quantity": 1, "visible": true, "platinum": 5, "order_type": "sell", "user": { "reputation": 4, "region": "en", "last_seen": "2022-11-18T18:31:49.199+00:00", "ingame_name": "TheronGuardxx", "avatar": "user/avatar/5e235e94ab7656047a86f70c.png?7b1e90d474a62c6ba3c2d3ef06aed927", "id": "5e235e94ab7656047a86f70c", "status": "offline" }, "platform": "pc", "region": "en", "creation_date": "2020-12-17T22:46:57.000+00:00", "last_update": "2022-10-15T23:37:01.000+00:00", "id": "5fdbdfe13e8c4f017f5e3352" } ] } }
需要查找该JSON中platinum字段的最小值,已知思路是循环遍历数据,当当前platinum值小于min变量现有值时更新min,但不清楚具体实现。目前已编写一段从JSON末尾元素筛选符合条件条目的代码:
num = 1 flagSell = 0 while flagSell == 0: if r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["user"]['status'] == 'ingame': if r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["region"] == 'en': if r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["order_type"] == 'sell': min = r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["platinum"] author = r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["user"]["ingame_name"] quantity = r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["quantity"] flagSell = 1 else: num += 1 else: num += 1 else: num += 1
请提供查找platinum最小值的正确Python代码实现方案。
解决方案
这里提供两种实现方式,分别对应基础循环思路和Python内置函数的简洁写法:
方式一:基础循环实现(匹配你最初的思路)
手动遍历所有订单,维护最小值变量并逐步更新,同时可同步记录对应卖家信息:
# 假设r_json是已解析完成的JSON数据对象 orders = r_json["payload"]["orders"] # 初始化最小值为第一个订单的platinum值,同步记录对应信息 min_platinum = orders[0]["platinum"] min_seller = orders[0]["user"]["ingame_name"] min_quantity = orders[0]["quantity"] # 从第二个订单开始遍历比较 for order in orders[1:]: current_plat = order["platinum"] if current_plat < min_platinum: min_platinum = current_plat min_seller = order["user"]["ingame_name"] min_quantity = order["quantity"] # 输出结果 print(f"最低Platinum价格: {min_platinum}") print(f"对应卖家: {min_seller}") print(f"出售数量: {min_quantity}")
如果需要仅筛选符合特定条件的订单(和你现有代码逻辑一致,筛选status='ingame'、region='en'、order_type='sell'的条目),可在循环中加入条件判断:
orders = r_json["payload"]["orders"] min_platinum = None min_seller = None min_quantity = None for order in orders: # 检查是否符合筛选条件 if (order["user"]["status"] == "ingame" and order["region"] == "en" and order["order_type"] == "sell"): current_plat = order["platinum"] # 首次找到符合条件的订单时初始化最小值,后续更新更小值 if min_platinum is None or current_plat < min_platinum: min_platinum = current_plat min_seller = order["user"]["ingame_name"] min_quantity = order["quantity"] # 根据结果输出 if min_platinum is not None: print(f"符合条件的最低Platinum价格: {min_platinum}") print(f"对应卖家: {min_seller}") print(f"出售数量: {min_quantity}") else: print("未找到符合条件的订单")
方式二:使用Python内置min()函数(更简洁高效)
利用min()函数的key参数,可直接获取platinum值最小的订单,代码更简洁:
orders = r_json["payload"]["orders"] # 获取platinum值最小的订单对象 min_order = min(orders, key=lambda x: x["platinum"]) # 输出结果 print(f"最低Platinum价格: {min_order['platinum']}") print(f"对应卖家: {min_order['user']['ingame_name']}") print(f"出售数量: {min_order['quantity']}")
如果需要筛选特定条件的订单,先过滤再取最小值:
orders = r_json["payload"]["orders"] # 先筛选符合条件的订单 filtered_orders = [ order for order in orders if (order["user"]["status"] == "ingame" and order["region"] == "en" and order["order_type"] == "sell") ] # 若有符合条件的订单则取最小值,否则提示无结果 if filtered_orders: min_order = min(filtered_orders, key=lambda x: x["platinum"]) print(f"符合条件的最低Platinum价格: {min_order['platinum']}") print(f"对应卖家: {min_order['user']['ingame_name']}") print(f"出售数量: {min_order['quantity']}") else: print("未找到符合条件的订单")
内容的提问来源于stack exchange,提问作者KoraOnEarth
相关产品推荐
相关产品推荐

