Lis*_*adk 0 python floating-point if-statement
我有一个浮动列表,看起来像这样:
predictions_dec = [13.0, 8.6, 4.9, -1.5, 6.2, 7.7, 2.0, 10.0, 7.7, 12.7,...]
Run Code Online (Sandbox Code Playgroud)
我想清理这些数据,方法是给出高于10.0的数字10.0和低于0.0的数字(所以所有负数)为0.0.我正在使用以下if语句执行此操作:
predictions_clean = []
for pred in predictions_dec:
if pred >= 10:
predictions_clean.append(10.0)
if pred <= 0:
predictions_clean.append(0.0)
else:
predictions_clean.append(pred)
Run Code Online (Sandbox Code Playgroud)
这段代码似乎有效,但有趣的是:
len(predictions_dec)
1222
len(predictions_clean)
1816
Run Code Online (Sandbox Code Playgroud)
我对if语句的理解并不是那么好.在if语句中,我做错了什么?
您需要更换第二if有elif:
predictions_clean = []
for pred in predictions_dec:
if pred >= 10:
predictions_clean.append(10.0)
elif pred <= 0:
predictions_clean.append(0.0)
else:
predictions_clean.append(pred)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76 次 |
| 最近记录: |