【问题标题】:Can't find the lowest number value in this dict在这个字典中找不到最小的数值
【发布时间】:2021-11-21 21:59:47
【问题描述】:

我试图在这个字典中找到最小的数值 1.12498

这是我的代码:

data = {'instrument': 'EUR_USD', 'granularity': 'H1', 'candles': [{'complete': True, 'volume': 7787, 'time': '2021-11-19T10:00:00.000000000Z', 'bid': {'o': '1.12987', 'h': '1.13046', 'l': '1.12830', 'c': '1.13000'}, 'mid': {'o': '1.12994', 'h': '1.13052', 'l': '1.12838', 'c': '1.13008'}, 'ask': {'o': '1.13001', 'h': '1.13059', 'l': '1.12846', 'c': '1.13015'}}, {'complete': True, 'volume': 5721, 'time': '2021-11-19T11:00:00.000000000Z', 'bid': {'o': '1.13002', 'h': '1.13023', 'l': '1.12839', 'c': '1.12892'}, 'mid': {'o': '1.13009', 'h': '1.13030', 'l': '1.12846', 'c': '1.12900'}, 'ask': {'o': '1.13016', 'h': '1.13037', 'l': '1.12853', 'c': '1.12907'}}, {'complete': True, 'volume': 7464, 'time': '2021-11-19T12:00:00.000000000Z', 'bid': {'o': '1.12894', 'h': '1.12968', 'l': '1.12516', 'c': '1.12565'}, 'mid': {'o': '1.12902', 'h': '1.12975', 'l': '1.12524', 'c': '1.12574'}, 'ask': {'o': '1.12909', 'h': '1.12982', 'l': '1.12533', 'c': '1.12583'}}, {'complete': True, 'volume': 9911, 'time': '2021-11-19T13:00:00.000000000Z', 'bid': {'o': '1.12565', 'h': '1.12935', 'l': '1.12488', 'c': '1.12873'}, 'mid': {'o': '1.12572', 'h': '1.12942', 'l': '1.12498', 'c': '1.12880'}, 'ask': {'o': '1.12580', 'h': '1.12950', 'l': '1.12508', 'c': '1.12887'}}, {'complete': True, 'volume': 11112, 'time': '2021-11-19T14:00:00.000000000Z', 'bid': {'o': '1.12874', 'h': '1.13206', 'l': '1.12870', 'c': '1.13066'}, 'mid': {'o': '1.12882', 'h': '1.13214', 'l': '1.12878', 'c': '1.13074'}, 'ask': {'o': '1.12889', 'h': '1.13223', 'l': '1.12885', 'c': '1.13081'}}, {'complete': True, 'volume': 8523, 'time': '2021-11-19T15:00:00.000000000Z', 'bid': {'o': '1.13065', 'h': '1.13208', 'l': '1.12996', 'c': '1.13172'}, 'mid': {'o': '1.13072', 'h': '1.13215', 'l': '1.13003', 'c': '1.13178'}, 'ask': {'o': '1.13079', 'h': '1.13223', 'l': '1.13009', 'c': '1.13184'}}, {'complete': True, 'volume': 5485, 'time': '2021-11-19T16:00:00.000000000Z', 'bid': {'o': '1.13170', 'h': '1.13215', 'l': '1.13122', 'c': '1.13178'}, 'mid': {'o': '1.13176', 'h': '1.13222', 'l': '1.13128', 'c': '1.13184'}, 'ask': {'o': '1.13183', 'h': '1.13229', 'l': '1.13135', 'c': '1.13190'}}, {'complete': True, 'volume': 6994, 'time': '2021-11-19T17:00:00.000000000Z', 'bid': {'o': '1.13176', 'h': '1.13191', 'l': '1.12916', 'c': '1.12977'}, 'mid': {'o': '1.13183', 'h': '1.13196', 'l': '1.12924', 'c': '1.12984'}, 'ask': {'o': '1.13190', 'h': '1.13203', 'l': '1.12930', 'c': '1.12990'}}, {'complete': True, 'volume': 4885, 'time': '2021-11-19T18:00:00.000000000Z', 'bid': {'o': '1.12977', 'h': '1.13022', 'l': '1.12861', 'c': '1.12864'}, 'mid': {'o': '1.12985', 'h': '1.13029', 'l': '1.12868', 'c': '1.12870'}, 'ask': {'o': '1.12993', 'h': '1.13036', 'l': '1.12874', 'c': '1.12877'}}, {'complete': True, 'volume': 3054, 'time': '2021-11-19T19:00:00.000000000Z', 'bid': {'o': '1.12863', 'h': '1.12914', 'l': '1.12848', 'c': '1.12888'}, 'mid': {'o': '1.12870', 'h': '1.12922', 'l': '1.12855', 'c': '1.12895'}, 'ask': {'o': '1.12876', 'h': '1.12929', 'l': '1.12862', 'c': '1.12902'}}, {'complete': True, 'volume': 2292, 'time': '2021-11-19T20:00:00.000000000Z', 'bid': {'o': '1.12885', 'h': '1.12911', 'l': '1.12854', 'c': '1.12875'}, 'mid': {'o': '1.12893', 'h': '1.12918', 'l': '1.12862', 'c': '1.12884'}, 'ask': {'o': '1.12901', 'h': '1.12926', 'l': '1.12870', 'c': '1.12892'}}, {'complete': True, 'volume': 1028, 'time': '2021-11-19T21:00:00.000000000Z', 'bid': {'o': '1.12874', 'h': '1.12892', 'l': '1.12795', 'c': '1.12808'}, 'mid': {'o': '1.12882', 'h': '1.12900', 'l': '1.12808', 'c': '1.12819'}, 'ask': {'o': '1.12890', 'h': '1.12910', 'l': '1.12820', 'c': '1.12830'}}]}

lowest_key = min(data, key=data.get)

lowest_value = data[lowest_key]
print(lowest_value)

但它给了我这个错误:TypeError: '<' not supported between instances of 'list' and 'str'

我认为它不起作用,因为字典上有列表

【问题讨论】:

  • 不是1.12488 低吗?
  • 这是一个嵌套结构。您可以展平所有内容,然后在其中找到它出现的最小值,但是将布尔值与字符串进行比较时会遇到问题。似乎这些数字主要集中在列表中每根蜡烛的bidmidask 的字典中。那是你要提取的吗?
  • @Reti43 我只想从所有mid dict中的所有数字中提取最小的数字

标签: python json dictionary


【解决方案1】:
from itertools import chain

min(chain(*(candle['mid'].values() for candle in data['candles'])))

values = (candle['mid'].values() for candle in data['candles']) 提取每根蜡烛的mid 字典的值。然后你可以用你最喜欢的方式flatten the list of lists然后找到最小值。

【讨论】:

  • 很好的答案,我在过去的 10 分钟里一直在解决这个问题,并且正在使用函数来排序和循环,好的旧 itertools..... :D
猜你喜欢
  • 1970-01-01
  • 2023-04-08
  • 2015-01-16
  • 2020-07-26
  • 2021-07-16
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多