简体   繁体   中英

How I can subtract the smallest from the largest json file?

I am trying to output the largest and smallest number from a JSON file and subtract the smallest from the largest. Please give me a hand.My eror:max_price = max(price) TypeError: 'float' object is not iterable

My code:

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

Something like the below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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