为什么当“ if”中的条件不满足时,此代码永远不会进入“ else”并相应地打印?
j=0
for i in data:
if j<10:
if i['product']['id'] == p_id:
if (i['stop_price']!='None'):
print("Order Type:" + str(i['stop_order_type']))
print("Stop Price: " + str(i['stop_price']))
else:
print("Order Type: " + str(i['order_type']))
print("Limit Price: " + str(i['limit_price']))
print("Side: " + str(i['side']))
print("Size: " + str(i['size']))
print("Unfilled Size: " + str(i['unfilled_size']))
print("\n\n")
j+=1
它显示以下输出:
Order Type:stop_loss_order Stop Price: 405.0 Limit Price: 400.0 Side: buy Size: 1 Unfilled Size: 1 Order Type:None Stop Price: None Limit Price: 280.0 Side: sell Size: 1 Unfilled Size: 0 Order Type:None Stop Price: None Limit Price: 300.0 Side: sell Size: 1 Unfilled Size: 1
但是正确的输出应该是:
Order Type:stop_loss_order Stop Price: 405.0 Limit Price: 400.0 Side: buy Size: 1 Unfilled Size: 1 Order Type:Limit Limit Price: 280.0 Side: sell Size: 1 Unfilled Size: 0 Order Type:Limit Limit Price: 300.0 Side: sell Size: 1 Unfilled Size: 1
答案 0 :(得分:1)
将df['new'] = df.eq(1).dot(df.columns + ',').str.rstrip(',').str.split(',')
print (df)
A B C D E F new
0 a 4 7 1 5 a [D]
1 b 1 1 1 1 a [B, C, D, E]
2 c 4 9 5 6 a []
3 d 5 4 7 9 b []
4 e 5 2 1 1 b [D, E]
5 f 1 3 1 4 b [B, D]
更改为裸露的'None'
。您正在将其与字符串None
进行比较,除非它实际上是字符串'None'
,否则它通常为False
。
它应该像这样:
'None'
请注意,在与 if (i['stop_price'] != None):
进行比较的情况下,这样做的效率略高:
None
Thierry Lathuille指出PEP 8 Programming Recommendations建议使用后者。两种版本的行为应相同。
答案 1 :(得分:0)
如果(i ['stop_price']!='None'),请检查i ['stop_price']的值和类型,并相应地修改条件。
此处您要检查i ['stop_price']的值是否不等于String类型的“ None”,除非i ['stop_price']中的实际值等于“ None”,否则该值始终为True ',类型为String。