我们的目标是有一个list1:11,其中x-2对应于每个小于5的数字。
lst = list[1,11]
lst
[1,2,3,4,5,6,7,8,9,10]这就是我要做的。已尝试:
list(map(lambda x: x-2 for x<5 in lst, lst))
list(map(x-2, filter(lambda x: x<5 in lst))
list(if x < 5 in lst(map(lambda x: x-2)))我对Python语法非常陌生。我在上面所做的尝试以及其他许多类似的方法在SyntaxError中都会得到结果。如果我在描述我的问题时犯了错误,我很抱歉,因为我也是Stack的新手。
发布于 2020-10-27 22:16:19
一种可能的解决方案是:
solution = list(map(lambda x: x-2 if x < 5 else x, lst))发布于 2020-10-27 22:16:05
以下是一种可能的解决方案:
result = list(map(lambda x: x-2 if x<5 else x, lst))这是另一个可能的解决方案:
result = list(map(lambda x: x-2*(x<5), lst))以下是您想要的输出(使用lst = list(range(1, 11))):
>>> result
[-1, 0, 1, 2, 5, 6, 7, 8, 9, 10]https://stackoverflow.com/questions/64555937
复制相似问题