如何绝对确定列表中的负数?

时间:2018-09-18 13:28:04

标签: python

嗨,我如何在python中实现abs()函数以确保列表中的负值现在为正并存储到另一个变量中?

说...

sample = [-1, -3, 2, 5]

并且我想要变量sample1中新的正值列表,那该怎么办?

sample1 = map(abs, sample)
print(sample1)

1 个答案:

答案 0 :(得分:0)

>>> sample = [-1, -3, 2, 5]
>>> sample1 = [abs(x) for x in sample]
>>> sample1
[1, 3, 2, 5]