-
BIG阳
这应该工作:data = { "shopName": "Shop", "promotions": [ { "productName": "Cookies", "oldPrice": 11.99, "newPrice": 7.99, "discount": 33 }, { "productName": "Butter", "oldPrice": 27.15, "newPrice": 21.99, "discount": 19 }, { "productName": "Milk", "oldPrice": 30.45, "newPrice": 21.99, "discount": 27 } ]}MIN_PRICE = 20filtered_products = [p for p in data['promotions'] if p['discount'] >= MIN_PRICE]print(filtered_products)这打印:[ { "productName": "Cookies", "oldPrice": 11.99, "newPrice": 7.99, "discount": 33 }, { "productName": "Milk", "oldPrice": 30.45, "newPrice": 21.99, "discount": 27 }]另一种方法是使用filter函数:filtered_products = list(filter(lambda p: p['discount'] > MIN_PRICE, data['promotions']))
-
MMMHUHU
如果你已经解析了 JSON,你可以使用这个from typing import Dict, Anyparsed_json = {"shopName": "Shop","promotions": [ {"productName": "Cookies", "oldPrice": 11.99, "newPrice": 7.99, "discount": 33}, {"productName": "Butter", "oldPrice": 27.15, "newPrice": 21.99, "discount": 19}, {"productName": "Milk", "oldPrice": 30.45, "newPrice": 21.99, "discount": 27}, ],}def find_specific_data(num: int, data: Dict[Any, Any]) -> Dict[Any, Any]: for value in data["promotions"]: if value["discount"] > num: print(value)find_specific_data(26, parsed_json)In: find_specific_data(26)Out: {'productName': 'Cookies', 'oldPrice': 11.99, 'newPrice': 7.99, 'discount': 33} {'productName': 'Milk', 'oldPrice': 30.45, 'newPrice': 21.99, 'discount': 27}
-
摇曳的蔷薇
试试这个。import json, pandas as pddf=pd.DataFrame(json.loads('{ "shopName": "Shop", "promotions": [ { "productName": "Cookies", "oldPrice": 11.99, "newPrice": 7.99, "discount": 33 }, { "productName": "Butter", "oldPrice": 27.15, "newPrice": 21.99, "discount": 19 }, { "productName": "Milk", "oldPrice": 30.45, "newPrice": 21.99, "discount":27 } ]}')['promotions'])print(df) productName oldPrice newPrice discount0 Cookies 11.99 7.99 331 Butter 27.15 21.99 192 Milk 30.45 21.99 27print(df[df.discount==df.discount.max()]) productName oldPrice newPrice discount0 Cookies 11.99 7.99 33