How to turn all numbers in a list into their negative counterparts?

前端 未结 5 596
别跟我提以往
别跟我提以往 2020-12-10 13:48

I am trying to turn a list of positive numbers into a list of negative numbers with the same value in python 3.3.3

For example turning [1,2,3] into

5条回答
  •  孤城傲影
    2020-12-10 14:34

    For large list, you will probably better use numpy

    import numpy as np
    
    a=np.array([1,2,3,4])
    
    # result as a numpy array
    b=-a
    
    # can be casted back to list
    c=list(b)
    

提交回复
热议问题