拆分包含多个字典的列表

时间:2019-01-28 13:22:18

标签: python python-3.x list dictionary

我正在尝试将列表分成列表中包含的字典的数量。...下面是一个包含3个字典的列表的示例- 代码:

def open_orders_order(self,ticker):
        filter = json.dumps({"open": True})
        column = 'orderQty'
        open = client.Order.Order_getOrders(symbol=ticker, filter=filter, count=100, reverse=True).result()
        print(open[0])

这将导致输出:

[{'orderID': 'eb5ba1c3-3506-a55e7-5fe7-fa6e654933a7', 'clOrdID': '', 'clOrdLinkID': '', 'account': 24565, 'symbol': 'XBTUSD', 'side': 'Buy', 'simpleOrderQty': None, 'orderQty': 10, 'price': 2900.0, 'displayQty': None, 'stopPx': None, 'pegOffsetValue': None, 'pegPriceType': '', 'currency': 'USD', 'settlCurrency': 'XBt', 'ordType': 'Limit', 'timeInForce': 'GoodTillCancel', 'execInst': '', 'contingencyType': '', 'exDestination': 'XBME', 'ordStatus': 'New', 'triggered': '', 'workingIndicator': True, 'ordRejReason': '', 'simpleLeavesQty': None, 'leavesQty': 10, 'simpleCumQty': None, 'cumQty': 0, 'avgPx': None, 'multiLegReportingType': 'SingleSecurity', 'text': 'Submission from testnet.bitmex.com', 'transactTime': datetime.datetime(2019, 1, 28, 13, 9, 6, 37000, tzinfo=tzutc()), 'timestamp': datetime.datetime(2019, 1, 28, 13, 9, 6, 37000, tzinfo=tzutc())}, {'orderID': 'dfdbd030-87db-ees58f-93c0-76f03b9c9151', 'clOrdID': '', 'clOrdLinkID': '', 'account': 24565, 'symbol': 'XBTUSD', 'side': 'Buy', 'simpleOrderQty': None, 'orderQty': 10, 'price': 2950.0, 'displayQty': None, 'stopPx': None, 'pegOffsetValue': None, 'pegPriceType': '', 'currency': 'USD', 'settlCurrency': 'XBt', 'ordType': 'Limit', 'timeInForce': 'GoodTillCancel', 'execInst': '', 'contingencyType': '', 'exDestination': 'XBME', 'ordStatus': 'New', 'triggered': '', 'workingIndicator': True, 'ordRejReason': '', 'simpleLeavesQty': None, 'leavesQty': 10, 'simpleCumQty': None, 'cumQty': 0, 'avgPx': None, 'multiLegReportingType': 'SingleSecurity', 'text': 'Submission from testnet.bitmex.com', 'transactTime': datetime.datetime(2019, 1, 28, 12, 25, 46, 138000, tzinfo=tzutc()), 'timestamp': datetime.datetime(2019, 1, 28, 12, 25, 46, 138000, tzinfo=tzutc())}, {'orderID': '6bbb1820-efawhe0-a6ec-29d8-308297002eeb', 'clOrdID': '', 'clOrdLinkID': '', 'account': 24565,'symbol': 'XBTUSD', 'side': 'Buy', 'simpleOrderQty': None, 'orderQty': 10, 'price': 3000.0, 'displayQty': None, 'stopPx': None, 'pegOffsetValue': None, 'pegPriceType': '', 'currency': 'USD', 'settlCurrency': 'XBt', 'ordType': 'Limit', 'timeInForce': 'GoodTillCancel', 'execInst': '', 'contingencyType': '', 'exDestination': 'XBME', 'ordStatus': 'New', 'triggered': '', 'workingIndicator': True, 'ordRejReason': '', 'simpleLeavesQty': None, 'leavesQty': 10, 'simpleCumQty': None, 'cumQty': 0, 'avgPx': None, 'multiLegReportingType': 'SingleSecurity', 'text': 'Submission from testnet.bitmex.com', 'transactTime': datetime.datetime(2019, 1, 28, 12, 25, 38, 238000, tzinfo=tzutc()), 'timestamp': datetime.datetime(2019, 1, 28, 12, 25, 38, 238000, tzinfo=tzutc())}]

我想要3个不同的字典(因为此列表包含3个字典),以便我可以访问其中的值。

我尝试使用

对列表进行排序
 sorted_open =sorted(open[0])
 print(sorted_open) 

这将导致错误:

Traceback (most recent call last):
  File "C:\Users\Abhishek\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 62, in wrapped
    ret = await coro(*args, **kwargs)
  File "F:\Test files\Discord_bot\Test.py", line 815, in oo
    open_order = ord.open_orders_order('XBTUSD')
  File "F:\Test files\Discord_bot\Test_orders.py", line 98, in open_orders_order
    sorted_open =sorted(open[0])
TypeError: '<' not supported between instances of 'dict' and 'dict'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Abhishek\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\bot.py", line 898, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Abhishek\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 615, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Abhishek\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 71, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: '<' not supported between instances of 'dict' and 'dict'

我尝试了很多其他东西,但无法解决

2 个答案:

答案 0 :(得分:2)

尝试一下:

result = sorted(open[0], key=lambda x: x['orderID'])

或:

from operator import itemgetter

result = sorted(open[0], key=itemgetter('orderID'))

答案 1 :(得分:1)

您需要排序功能(带有orderID的示例):

export interface ObjectA {
    Test: string;
    Value: string;
}