[英]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.