如何得到lis的最大值

2024-07-05 10:14:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望得到最高的“高”出下面的dict。你知道吗

响应=

 [  
       {  
          'timestamp':'2019-04-13T04:12:00.000Z',
          'symbol':'XBTUSD',
          'open':5065,
          'high':5067,
          'low':5065.5,
          'close':5066.5,
          'trades':13,
          'volume':10002,
          'vwap':5066.8829,
          'lastSize':2,
          'turnover':197408849,
          'homeNotional':1.9740884899999998,
          'foreignNotional':10002
       },
       {  
          'timestamp':'2019-04-13T04:11:00.000Z',
          'symbol':'XBTUSD',
          'open':5065,
          'high':5065,
          'low':5065,
          'close':5065,
          'trades':0,
          'volume':0,
          'vwap':None,
          'lastSize':None,
          'turnover':0,
          'homeNotional':0,
          'foreignNotional':0
       },
       {  
          'timestamp':'2019-04-13T04:10:00.000Z',
          'symbol':'XBTUSD',
          'open':5065,
          'high':5065,
          'low':5065,
          'close':5065,
          'trades':2,
          'volume':2000,
          'vwap':5065,
          'lastSize':397,
          'turnover':39486000,
          'homeNotional':0.39486,
          'foreignNotional':2000
       }
    ]

然后要打印所有“high”:

for h in response:
   print (h['high'])

打印内容:

5067个 5065 5065

然后问题就出现了,我如何从数字列表中得到最大值?在这种情况下应该是“5067”。我试过用max方法,但没有用。(max(h['high']))不起作用。你知道吗


Tags: nonecloseopentradessymboltimestamplowhigh
3条回答

max(iterable,*[,key,default])-返回iterable中最大的项或两个或多个参数中最大的一个。你知道吗

b=max(a, key=lambda x:x['high'])
print(b['high'])

使用itemgetterkey参数:

from operator import itemgetter

max(h, key=itemgetter('high'))

可以使用list comprehensionhigh键获取所有值,然后使用max()函数获取最大值

maximum = max([h['high'] for h in response])
print(maximum)

5067

相关问题 更多 >