我有以下嵌套词典:
import tensorflow as tf
def condition(i, imgs_combined):
return tf.less(i, 5)
def body(i, imgs_combined):
c_image = tf.zeros(shape=(224, 224, 3),dtype=tf.float32)
imgs_combined = imgs_combined.write(i, c_image)
return [tf.add(i, 1), imgs_combined]
i = tf.constant(0)
imgs_combined = tf.TensorArray(dtype=tf.float32,size=1,dynamic_size=True,clear_after_read=False)
_, image_4d = tf.while_loop(condition,body,[i, imgs_combined])
image_4d = image_4d.stack()
with tf.Session() as sess:
image_4d_value = sess.run(image_4d)
print(image_4d_value.shape)
#print
(5, 224, 224, 3)
如果要满足两个条件:go._Order_Data_DB.items()
Out[62]: dict_items([(84852344, {'_action': 'OPEN', '_type': 0, '_symbol': 'EURUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 0}), (84852345, {'_action': 'CLOSE', '_type': 0, '_symbol': 'EURUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 84852345}),
(84852374, {'_action': 'OPEN', '_type': 0, '_symbol': 'GBPUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 84852345})])
和'_action' == 'OPEN'
,我想检索订单号(键)。我尝试使用下面的函数,但它仅查看第一个条件,如果有人知道如何使两个条件都起作用,那就太好了,因为它似乎忽略了“和”。
'_symbol' == ccy
答案 0 :(得分:0)
主要有两个问题:
if
语句可以从外部字典的值中查询键。return
将仅捕获第一个。要提取满足您条件的所有 项,请改为生成yield
,然后通过list
用尽生成器。这是一个示范;
d = dict([(84852344, {'_action': 'OPEN', '_type': 0, '_symbol': 'EURUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 0}),
(84852345, {'_action': 'CLOSE', '_type': 0, '_symbol': 'EURUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 84852345}),
(84852374, {'_action': 'OPEN', '_type': 0, '_symbol': 'GBPUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 84852345})])
def get_order_num(ccy):
for k, v in d.items():
if v['_symbol'] == ccy and v['_action'] == 'OPEN':
yield k
res1 = list(get_order_num('EURUSD')) # [84852344]
res2 = list(get_order_num('GBPUSD')) # [84852374]