Invert negative values in a list

后端 未结 5 559
清酒与你
清酒与你 2020-12-11 21:24

I am trying to convert a list that contains negative values, to a list of non-negative values; inverting the negative ones. I have tried abs but it didn\'t work

5条回答
  •  臣服心动
    2020-12-11 22:08

    The simple pythonic way is the list comprehension above but if you're using Numpy for anything else you could do:

    x2 = numpy.abs(x)
    

    with no need to convert or do any looping.

提交回复
热议问题