2013-05-05 64 views
-6

我有一个具有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] 
+0

真的,必须有一些你不告诉我们的限制,否则这将是太微不足道了。 – Cthulhu 2013-05-05 09:19:34

+3

我已经[已经告诉过你](http://stackoverflow.com/questions/16163394/turning-a-list-into-a-tuple-python#comment23098912_16163394)来研究,或尝试自己,然后再问。这是一个非常简单的问题。 – Volatility 2013-05-05 09:21:50

+0

这是[不是一个有效的列表理解](http://carlgroner.me/Python/2011/11/09/An-Introduction-to-List-Comprehensions-in-Python.html) – Volatility 2013-05-05 09:27:44

回答

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] 
0
[0.4 if 0 <= x <= 0.5 else 0.7 if 0.6 <= x <= 1 else DEFAULT_VAL for x in L]