繁体   English   中英

如何从最大的 json 文件中减去最小的?

[英]How I can subtract the smallest from the largest json file?

我正在尝试 output JSON文件中的最大和最小数字,并从最大数字中减去最小数字。 请帮帮我。我的错误:max_price = max(price) TypeError: 'float' object is not iterable

我的代码:

with open(test) as file:
    data = json.load(file)

    for items in data['marketPairs']:
        price = items['price']
        if price <= price or price >= price:
            max_price =  max(price)
            min_price = min(price)

            diff = max_price - min_price

            print(diff)

```json
{
    "name_of_coin": "ZUSD",
    "marketPairs": [
        {
            "exchange_name": "Liquid",
            "market_url": "https://app.liquid.com/exchange/ZUSDUSD",
            "price": 0.9995,
            "last_update": "2021-12-05T16:07:54.000Z",
            "exchange_id": 112
        },
        {
            "exchange_name": "Liquid",
            "market_url": "https://app.liquid.com/exchange/USDTZUSD",
            "price": 0.9988557475390971,
            "last_update": "2021-12-05T16:07:54.000Z",
            "exchange_id": 112
        }
     ]
}

类似下面的东西

data = {'marketPairs':[{'price':3},{'price':33}]}
prices = [i['price'] for i in data['marketPairs']]
_min = min(data['marketPairs'],key = lambda x: x['price'])['price']
_max = max(data['marketPairs'],key = lambda x: x['price'])['price']
delta = _max - _min
print(delta)

output

30

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM