简体   繁体   English

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

[英]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.我正在尝试 output JSON文件中的最大和最小数字,并从最大数字中减去最小数字。 Please give me a hand.My eror:max_price = max(price) TypeError: 'float' object is not iterable请帮帮我。我的错误: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 output

30

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

相关问题 如何对两个参数进行排序 应该先显示最大的数字,然后从小到大 - How do I sort two parameters The largest number should be displayed first, and then from the smallest to the largest 如何从最小到最大循环? - how to make loops from smallest to largest number? 如何从列表中减去列表中的最小值? - How to subtract from a list by the smallest value in the list? 如何使用不使用 min() 或 max() 的输入找到最小和最大数 - How can I find smallest and largest number with inputs not using min() nor max() 如何使用while循环从列表中的每个整数中减去最小的数字? - How do I subtract the smallest number from each integer in a list using a while loop? 我如何在 Python 中的列表中找到最小值或最大值 - How do i find the smallest or largest value in a list in Python 如何将项目从最大值到最小值(python) - How to sum items from largest value to smallest (python) 如何使用 Python 从最大到最小对可能的数据进行排序 - How to sort may data from largest to smallest using Python 如何对 dataframe 中的列进行排序,使第一行中的值从大到小? - How to sort columns in a dataframe such that the values in the first row are from largest to smallest? 如何从最小到最大对 dataframe 中的每一行进行排序 - How to sort each row in a dataframe from the smallest to largest number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM