[英]How do I extract a dictionary from a list which only has that dictionary in it?
我正在尝试制作一个算法交易程序,并且
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': '', 'roll': 0, 'com': 0, 'open': 0, 'valueDate': '', 'grossPL': 1298.32056, 'close': 0, 'visiblePL': 9.3, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': False, 'amountK': 1000, 'currencyPoint': 0, 'time': None, 'usedMargin': 0, 'stop': 0, 'stopMove': 0, 'limit': 0, 'isTotal': True}
问题是,当我将上面的代码块放入我的实际程序中时,它陷入了一个奇怪的循环并且永远不会出现。 所以,我试图找到一种方法,在不使用循环的情况下将真正的字典(第一个)从列表中分离出来。 任何帮助将不胜感激。 非常感谢!
*列表
[{'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': '', 'roll': 0, 'com': 0, 'open': 0, 'valueDate': '', 'grossPL': 433.14843, 'close': 0, 'visiblePL': 3.1, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': False, 'amountK': 1000, 'currencyPoint': 0, 'time': None, 'usedMargin': 0, 'stop': 0, 'stopMove': 0, 'limit': 0, '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': '', 'roll': 0, 'com': 0, 'open': 0, 'valueDate': '', 'grossPL': 433.14843, 'close': 0, 'visiblePL': 3.1, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': False, 'amountK': 1000, 'currencyPoint': 0, 'time': None, 'usedMargin': 0, 'stop': 0, 'stopMove': 0, 'limit': 0, 'isTotal': True}]
dict1 然后会给你:
{'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}
或者你可以做这样的事情:
L = [{'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': '', 'roll': 0, 'com': 0, 'open': 0, 'valueDate': '', 'grossPL': 433.14843, 'close': 0, 'visiblePL': 3.1, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': False, 'amountK': 1000, 'currencyPoint': 0, 'time': None, 'usedMargin': 0, 'stop': 0, 'stopMove': 0, 'limit': 0, 'isTotal': True}]
L[0]
{'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}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.