所以我得到了这个字符串
[{"symbol":"XBTUSD","id":8799145800,"side":"Sell","size":53192,"price":8542},
{"symbol":"XBTUSD","id":8799145850,"side":"Sell","size":123925,"price":8541.5},
{"symbol":"XBTUSD","id":8799145900,"side":"Sell","size":11435,"price":8541},
{"symbol":"XBTUSD","id":8799145950,"side":"Sell","size":4710,"price":8540.5},
{"symbol":"XBTUSD","id":8799146000,"side":"Sell","size":79339,"price":8540},
{"symbol":"XBTUSD","id":8799146050,"side":"Sell","size":33137,"price":8539.5},
{"symbol":"XBTUSD","id":8799146100,"side":"Sell","size":39601,"price":8539},
{"symbol":"XBTUSD","id":8799146150,"side":"Sell","size":40,"price":8538.5},
{"symbol":"XBTUSD","id":8799146200,"side":"Sell","size":12023,"price":8538},
{"symbol":"XBTUSD","id":8799146250,"side":"Sell","size":720,"price":8537.5},
{"symbol":"XBTUSD","id":8799146300,"side":"Sell","size":672030,"price":8537},
{"symbol":"XBTUSD","id":8799146350,"side":"Sell","size":27574,"price":8536.5},
{"symbol":"XBTUSD","id":8799146400,"side":"Sell","size":10060,"price":8536},
{"symbol":"XBTUSD","id":8799146450,"side":"Sell","size":65500,"price":8535.5},
{"symbol":"XBTUSD","id":8799146500,"side":"Sell","size":15702,"price":8535},
{"symbol":"XBTUSD","id":8799146550,"side":"Sell","size":40084,"price":8534.5},
{"symbol":"XBTUSD","id":8799146600,"side":"Sell","size":9406,"price":8534},
{"symbol":"XBTUSD","id":8799146650,"side":"Sell","size":210,"price":8533.5},
{"symbol":"XBTUSD","id":8799146700,"side":"Sell","size":5847,"price":8533},
{"symbol":"XBTUSD","id":8799146800,"side":"Sell","size":81950,"price":8532},
{"symbol":"XBTUSD","id":8799146850,"side":"Sell","size":24162,"price":8531.5},
{"symbol":"XBTUSD","id":8799146900,"side":"Sell","size":250823,"price":8531},
{"symbol":"XBTUSD","id":8799146950,"side":"Sell","size":16125,"price":8530.5},
{"symbol":"XBTUSD","id":8799147000,"side":"Sell","size":77656,"price":8530},
{"symbol":"XBTUSD","id":8799147050,"side":"Sell","size":164557,"price":8529.5},
{"symbol":"XBTUSD","id":8799147100,"side":"Buy","size":71227,"price":8529},
{"symbol":"XBTUSD","id":8799147150,"side":"Buy","size":30650,"price":8528.5},
{"symbol":"XBTUSD","id":8799147250,"side":"Buy","size":16060,"price":8527.5},
{"symbol":"XBTUSD","id":8799147350,"side":"Buy","size":2100,"price":8526.5},
{"symbol":"XBTUSD","id":8799147400,"side":"Buy","size":1359,"price":8526},
{"symbol":"XBTUSD","id":8799147450,"side":"Buy","size":89,"price":8525.5},
{"symbol":"XBTUSD","id":8799147500,"side":"Buy","size":2200,"price":8525},
{"symbol":"XBTUSD","id":8799147600,"side":"Buy","size":23,"price":8524},
{"symbol":"XBTUSD","id":8799147700,"side":"Buy","size":1025,"price":8523},
{"symbol":"XBTUSD","id":8799147900,"side":"Buy","size":125,"price":8521},
{"symbol":"XBTUSD","id":8799147950,"side":"Buy","size":23,"price":8520.5},
{"symbol":"XBTUSD","id":8799148000,"side":"Buy","size":10001,"price":8520},
{"symbol":"XBTUSD","id":8799148050,"side":"Buy","size":171,"price":8519.5},
{"symbol":"XBTUSD","id":8799148100,"side":"Buy","size":625,"price":8519},
{"symbol":"XBTUSD","id":8799148150,"side":"Buy","size":590,"price":8518.5},
{"symbol":"XBTUSD","id":8799148200,"side":"Buy","size":4080,"price":8518},
{"symbol":"XBTUSD","id":8799148250,"side":"Buy","size":31585,"price":8517.5},
{"symbol":"XBTUSD","id":8799148300,"side":"Buy","size":42744,"price":8517},
{"symbol":"XBTUSD","id":8799148350,"side":"Buy","size":20459,"price":8516.5},
{"symbol":"XBTUSD","id":8799148400,"side":"Buy","size":28190,"price":8516},
{"symbol":"XBTUSD","id":8799148450,"side":"Buy","size":9220,"price":8515.5},
{"symbol":"XBTUSD","id":8799148500,"side":"Buy","size":102130,"price":8515},
{"symbol":"XBTUSD","id":8799148550,"side":"Buy","size":2702,"price":8514.5},
{"symbol":"XBTUSD","id":8799148600,"side":"Buy","size":3225,"price":8514},
{"symbol":"XBTUSD","id":8799148650,"side":"Buy","size":19085,"price":8513.5}]
我需要检索卖方行的最低价格和买方行的最高价格。要检索的值分别为8529.5和8529。
答案 0 :(得分:1)
以下使用两个排序列表推导来获取所需的值。我们有一个寻找关键方面的价值卖出,一个寻找买入。我们只是按排序列表的索引返回所需的值。
input = [{"symbol":"XBTUSD","id":8799145800,"side":"Sell","size":53192,"price":8542},
{"symbol":"XBTUSD","id":8799145850,"side":"Sell","size":123925,"price":8541.5},
{"symbol":"XBTUSD","id":8799145900,"side":"Sell","size":11435,"price":8541},
{"symbol":"XBTUSD","id":8799145950,"side":"Sell","size":4710,"price":8540.5},
{"symbol":"XBTUSD","id":8799146000,"side":"Sell","size":79339,"price":8540},
{"symbol":"XBTUSD","id":8799146050,"side":"Sell","size":33137,"price":8539.5},
{"symbol":"XBTUSD","id":8799146100,"side":"Sell","size":39601,"price":8539},
{"symbol":"XBTUSD","id":8799146150,"side":"Sell","size":40,"price":8538.5},
{"symbol":"XBTUSD","id":8799146200,"side":"Sell","size":12023,"price":8538},
{"symbol":"XBTUSD","id":8799146250,"side":"Sell","size":720,"price":8537.5},
{"symbol":"XBTUSD","id":8799146300,"side":"Sell","size":672030,"price":8537},
{"symbol":"XBTUSD","id":8799146350,"side":"Sell","size":27574,"price":8536.5},
{"symbol":"XBTUSD","id":8799146400,"side":"Sell","size":10060,"price":8536},
{"symbol":"XBTUSD","id":8799146450,"side":"Sell","size":65500,"price":8535.5},
{"symbol":"XBTUSD","id":8799146500,"side":"Sell","size":15702,"price":8535},
{"symbol":"XBTUSD","id":8799146550,"side":"Sell","size":40084,"price":8534.5},
{"symbol":"XBTUSD","id":8799146600,"side":"Sell","size":9406,"price":8534},
{"symbol":"XBTUSD","id":8799146650,"side":"Sell","size":210,"price":8533.5},
{"symbol":"XBTUSD","id":8799146700,"side":"Sell","size":5847,"price":8533},
{"symbol":"XBTUSD","id":8799146800,"side":"Sell","size":81950,"price":8532},
{"symbol":"XBTUSD","id":8799146850,"side":"Sell","size":24162,"price":8531.5},
{"symbol":"XBTUSD","id":8799146900,"side":"Sell","size":250823,"price":8531},
{"symbol":"XBTUSD","id":8799146950,"side":"Sell","size":16125,"price":8530.5},
{"symbol":"XBTUSD","id":8799147000,"side":"Sell","size":77656,"price":8530},
{"symbol":"XBTUSD","id":8799147050,"side":"Sell","size":164557,"price":8529.5},
{"symbol":"XBTUSD","id":8799147100,"side":"Buy","size":71227,"price":8529},
{"symbol":"XBTUSD","id":8799147150,"side":"Buy","size":30650,"price":8528.5},
{"symbol":"XBTUSD","id":8799147250,"side":"Buy","size":16060,"price":8527.5},
{"symbol":"XBTUSD","id":8799147350,"side":"Buy","size":2100,"price":8526.5},
{"symbol":"XBTUSD","id":8799147400,"side":"Buy","size":1359,"price":8526},
{"symbol":"XBTUSD","id":8799147450,"side":"Buy","size":89,"price":8525.5},
{"symbol":"XBTUSD","id":8799147500,"side":"Buy","size":2200,"price":8525},
{"symbol":"XBTUSD","id":8799147600,"side":"Buy","size":23,"price":8524},
{"symbol":"XBTUSD","id":8799147700,"side":"Buy","size":1025,"price":8523},
{"symbol":"XBTUSD","id":8799147900,"side":"Buy","size":125,"price":8521},
{"symbol":"XBTUSD","id":8799147950,"side":"Buy","size":23,"price":8520.5},
{"symbol":"XBTUSD","id":8799148000,"side":"Buy","size":10001,"price":8520},
{"symbol":"XBTUSD","id":8799148050,"side":"Buy","size":171,"price":8519.5},
{"symbol":"XBTUSD","id":8799148100,"side":"Buy","size":625,"price":8519},
{"symbol":"XBTUSD","id":8799148150,"side":"Buy","size":590,"price":8518.5},
{"symbol":"XBTUSD","id":8799148200,"side":"Buy","size":4080,"price":8518},
{"symbol":"XBTUSD","id":8799148250,"side":"Buy","size":31585,"price":8517.5},
{"symbol":"XBTUSD","id":8799148300,"side":"Buy","size":42744,"price":8517},
{"symbol":"XBTUSD","id":8799148350,"side":"Buy","size":20459,"price":8516.5},
{"symbol":"XBTUSD","id":8799148400,"side":"Buy","size":28190,"price":8516},
{"symbol":"XBTUSD","id":8799148450,"side":"Buy","size":9220,"price":8515.5},
{"symbol":"XBTUSD","id":8799148500,"side":"Buy","size":102130,"price":8515},
{"symbol":"XBTUSD","id":8799148550,"side":"Buy","size":2702,"price":8514.5},
{"symbol":"XBTUSD","id":8799148600,"side":"Buy","size":3225,"price":8514},
{"symbol":"XBTUSD","id":8799148650,"side":"Buy","size":19085,"price":8513.5}]
lowSell = sorted([v['price'] for v in input if v['side'].lower() == 'sell'])[0]
highBuy = sorted([v['price'] for v in input if v['side'].lower() == 'buy'])[-1]
print(lowSell)
print(highBuy)
打印以下内容
8529.5
8529
答案 1 :(得分:0)
以下是我如何迭代数组中的每个字典,检查它是最高价还是最低价。请注意我如何索引循环中的字典。
my_list = [{"symbol":"XBTUSD","id":8799145800,"side":"Sell","size":53192,"price":8542},
{"symbol":"XBTUSD","id":8799145850,"side":"Sell","size":123925,"price":8541.5},
{"symbol":"XBTUSD","id":8799145900,"side":"Sell","size":11435,"price":8541},
{"symbol":"XBTUSD","id":8799145950,"side":"Sell","size":4710,"price":8540.5},
{"symbol":"XBTUSD","id":8799146000,"side":"Sell","size":79339,"price":8540},
{"symbol":"XBTUSD","id":8799146050,"side":"Sell","size":33137,"price":8539.5},
{"symbol":"XBTUSD","id":8799146100,"side":"Sell","size":39601,"price":8539},
{"symbol":"XBTUSD","id":8799146150,"side":"Sell","size":40,"price":8538.5},
{"symbol":"XBTUSD","id":8799146200,"side":"Sell","size":12023,"price":8538},
{"symbol":"XBTUSD","id":8799146250,"side":"Sell","size":720,"price":8537.5},
{"symbol":"XBTUSD","id":8799146300,"side":"Sell","size":672030,"price":8537},
{"symbol":"XBTUSD","id":8799146350,"side":"Sell","size":27574,"price":8536.5},
{"symbol":"XBTUSD","id":8799146400,"side":"Sell","size":10060,"price":8536},
{"symbol":"XBTUSD","id":8799146450,"side":"Sell","size":65500,"price":8535.5},
{"symbol":"XBTUSD","id":8799146500,"side":"Sell","size":15702,"price":8535},
{"symbol":"XBTUSD","id":8799146550,"side":"Sell","size":40084,"price":8534.5},
{"symbol":"XBTUSD","id":8799146600,"side":"Sell","size":9406,"price":8534},
{"symbol":"XBTUSD","id":8799146650,"side":"Sell","size":210,"price":8533.5},
{"symbol":"XBTUSD","id":8799146700,"side":"Sell","size":5847,"price":8533},
{"symbol":"XBTUSD","id":8799146800,"side":"Sell","size":81950,"price":8532},
{"symbol":"XBTUSD","id":8799146850,"side":"Sell","size":24162,"price":8531.5},
{"symbol":"XBTUSD","id":8799146900,"side":"Sell","size":250823,"price":8531},
{"symbol":"XBTUSD","id":8799146950,"side":"Sell","size":16125,"price":8530.5},
{"symbol":"XBTUSD","id":8799147000,"side":"Sell","size":77656,"price":8530},
{"symbol":"XBTUSD","id":8799147050,"side":"Sell","size":164557,"price":8529.5},
{"symbol":"XBTUSD","id":8799147100,"side":"Buy","size":71227,"price":8529},
{"symbol":"XBTUSD","id":8799147150,"side":"Buy","size":30650,"price":8528.5},
{"symbol":"XBTUSD","id":8799147250,"side":"Buy","size":16060,"price":8527.5},
{"symbol":"XBTUSD","id":8799147350,"side":"Buy","size":2100,"price":8526.5},
{"symbol":"XBTUSD","id":8799147400,"side":"Buy","size":1359,"price":8526},
{"symbol":"XBTUSD","id":8799147450,"side":"Buy","size":89,"price":8525.5},
{"symbol":"XBTUSD","id":8799147500,"side":"Buy","size":2200,"price":8525},
{"symbol":"XBTUSD","id":8799147600,"side":"Buy","size":23,"price":8524},
{"symbol":"XBTUSD","id":8799147700,"side":"Buy","size":1025,"price":8523},
{"symbol":"XBTUSD","id":8799147900,"side":"Buy","size":125,"price":8521},
{"symbol":"XBTUSD","id":8799147950,"side":"Buy","size":23,"price":8520.5},
{"symbol":"XBTUSD","id":8799148000,"side":"Buy","size":10001,"price":8520},
{"symbol":"XBTUSD","id":8799148050,"side":"Buy","size":171,"price":8519.5},
{"symbol":"XBTUSD","id":8799148100,"side":"Buy","size":625,"price":8519},
{"symbol":"XBTUSD","id":8799148150,"side":"Buy","size":590,"price":8518.5},
{"symbol":"XBTUSD","id":8799148200,"side":"Buy","size":4080,"price":8518},
{"symbol":"XBTUSD","id":8799148250,"side":"Buy","size":31585,"price":8517.5},
{"symbol":"XBTUSD","id":8799148300,"side":"Buy","size":42744,"price":8517},
{"symbol":"XBTUSD","id":8799148350,"side":"Buy","size":20459,"price":8516.5},
{"symbol":"XBTUSD","id":8799148400,"side":"Buy","size":28190,"price":8516},
{"symbol":"XBTUSD","id":8799148450,"side":"Buy","size":9220,"price":8515.5},
{"symbol":"XBTUSD","id":8799148500,"side":"Buy","size":102130,"price":8515},
{"symbol":"XBTUSD","id":8799148550,"side":"Buy","size":2702,"price":8514.5},
{"symbol":"XBTUSD","id":8799148600,"side":"Buy","size":3225,"price":8514},
{"symbol":"XBTUSD","id":8799148650,"side":"Buy","size":19085,"price":8513.5}]
lowestPrice = 10000000000 # a really large number to start
biggestPrice = 0 # a really low number to start
for each in my_list:
# check if its a sell row, if so, and its less, update the lowest
if each["side"] == "Sell" and each["price"] < lowestPrice:
lowestPrice = each["price"]
# check if its a buy row, if so, and its more, update the biggest
if each["side"] == "Buy" and each["price"] > biggestPrice:
biggestPrice = each["price"]
print(lowestPrice)
print(biggestPrice)