如何从仅包含该词典的列表中提取词典?

问题描述

我正在尝试制作算法交易程序,并且

open_positions = Trader.open_positions
for position in open_positions:
    print(position)

输出两个字典(?)

{'t': 1,'ratePrecision': 5,'TradeId': '32572646','accountName': '05654022','accountId': '5654022','roll': 0,'com': 0,'open': 0.71538,'valueDate': '','grosspL': 1298.32056,'close': 0.71631,'visiblePL': 9.3,'isdisabled': False,'currency': 'AUD/USD','isBuy': True,'amountK': 1000,'currencyPoint': 139.59559,'time': '10022020065344','usedMargin': 2500,'OpenorderRequestTXT': 'FXTC','stop': 0,'stopMove': 0,'limit': 0}
{'t': 1,'ratePrecision': 0,'TradeId': '','accountName': '','accountId': '','open': 0,'close': 0,'isBuy': False,'currencyPoint': 0,'time': None,'usedMargin': 0,'limit': 0,'isTotal': True}

问题是,当我将上述代码块放入我的实际程序中时,它陷入了一个怪异的循环,并且永远不会出现。因此,我试图找到一种无需使用循环即可从列表中分离出真正字典(第一个字典)的方法。任何帮助将不胜感激。非常感谢!

*列表

[{'t': 1,'grosspL': 433.14843,'close': 0.71569,'visiblePL': 3.1,'currencyPoint': 139.71652,'limit': 0},{'t': 1,'isTotal': True}]


    

解决方法

您可以做这样的事情

dict1,dict2 = [{'t': 1,'ratePrecision': 5,'tradeId': '32572646','accountName': '05654022','accountId': '5654022','roll': 0,'com': 0,'open': 0.71538,'valueDate': '','grossPL': 433.14843,'close': 0.71569,'visiblePL': 3.1,'isDisabled': False,'currency': 'AUD/USD','isBuy': True,'amountK': 1000,'currencyPoint': 139.71652,'time': '10022020065344','usedMargin': 2500,'OpenOrderRequestTXT': 'FXTC','stop': 0,'stopMove': 0,'limit': 0},{'t': 1,'ratePrecision': 0,'tradeId': '','accountName': '','accountId': '','open': 0,'close': 0,'isBuy': False,'currencyPoint': 0,'time': None,'usedMargin': 0,'limit': 0,'isTotal': True}]

dict1然后会给您:

{'t': 1,'limit': 0}

或者您可以执行以下操作:

L = [{'t': 1,'isTotal': True}]

L [0]

{'t': 1,'limit': 0}