为了账号安全,请及时绑定邮箱和手机立即绑定

Python JSON 查找特定数据

Python JSON 查找特定数据

小怪兽爱吃肉 2023-06-13 16:19:58
我有这个 JSON 文件结构(从网站解析的销售产品)。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  } ]}问题是如何只显示比给定数字更大的折扣的产品(具有所有特征:名称、旧价格、新价格、折扣)。
查看完整描述

3 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

这应该工作:


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 = 20


filtered_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']))



查看完整回答
反对 回复 2023-06-13
?
MMMHUHU

TA贡献1834条经验 获得超8个赞

如果你已经解析了 JSON,你可以使用这个


from typing import Dict, Any


parsed_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}


查看完整回答
反对 回复 2023-06-13
?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

试试这个。


import json, pandas as pd


df=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  discount

0     Cookies     11.99      7.99        33

1      Butter     27.15     21.99        19

2        Milk     30.45     21.99        27


print(df[df.discount==df.discount.max()])


 productName  oldPrice  newPrice  discount

0     Cookies     11.99      7.99        33


查看完整回答
反对 回复 2023-06-13
  • 3 回答
  • 0 关注
  • 123 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信