将列表的数量更改为另一个数字python

时间:2013-05-05 09:16:53

标签: python list

我的列表中包含从01的一系列数字:

[0.01,0.1,0.4,0.034,0.6,0.7,0.9,1]

我如何编辑列表以便将0-0.50.4的所有数字都更改为0.6-10.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]

2 个答案:

答案 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]