[Python] capitating a list of integers

larry price laprice at gmail.com
Sat May 12 00:08:04 PDT 2007


given a list of positive integers and a limit value return a list in
which all values greater than the limit are set to the limit.

here's what I came up with

>>> n
[1, 1, 1, 21, 2, 3, 4, 5, 55, 100, 99, 1005, 111, 11, 101]
>>> z = [(lambda x: x if x <100 else 100)(i) for i in n]
>>> z
[1, 1, 1, 21, 2, 3, 4, 5, 55, 100, 99, 100, 100, 11, 100]
>>>

is there a more elegant way to phrase that?


More information about the Python mailing list