我的列表中包含从0
到1
的一系列数字:
[0.01,0.1,0.4,0.034,0.6,0.7,0.9,1]
我如何编辑列表以便将0-0.5
到0.4
的所有数字都更改为0.6-1
到0.7
的所有数字,以便列表变为:
[0.4,0.40.40.4,0.7,0.7,0.7,0.7]
confindencenumbers = [(x=0.4) for x in confindencenumbers if x < 0.4]
confindencenumbers = [(x=0.7} for x in confindencenumbers if x > 0.5]
答案 0 :(得分:3)
>>> l = [0.01, 0.1, 0.4, 0.034, 0.6, 0.7, 0.9, 1]
>>> [0.4 if (0. < f < 0.5) else 0.7 for f in l]
[0.4, 0.4, 0.4, 0.4, 0.7, 0.7, 0.7, 0.7]
答案 1 :(得分:0)
[0.4 if 0 <= x <= 0.5 else 0.7 if 0.6 <= x <= 1 else DEFAULT_VAL for x in L]