[英]How do I sort two parameters The largest number should be displayed first, and then from the smallest to the largest
[英]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.