Python 如何从只包含该词典的列表中提取词典?
我正在尝试制作一个算法交易程序Python 如何从只包含该词典的列表中提取词典?,python,list,dictionary,for-loop,algorithmic-trading,Python,List,Dictionary,For Loop,Algorithmic Trading,我正在尝试制作一个算法交易程序 open_positions = trader.open_positions for position in open_positions: print(position) 输出两个字典(?) 问题是,当我把上面的代码块放到我的实际程序中时,它会陷入一个奇怪的循环,永远不会出现。因此,我试图找到一种方法,在不使用循环的情况下,将真正的字典(第一个)从列表中分离出来。任何帮助都将不胜感激。多谢各位 *名单 [{'t': 1, 'ratePrecision'
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': 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]
如果你只对某个列表中的某个特定项目感兴趣,那么就没有必要在整个列表中循环并从列表中获取该项目。只需访问与您要访问的项目的相关索引号相关的列表。例如,如果您确定
open\u positions
实际上是一个列表,open\u positions[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}]
{'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}