Raising elements of a list to a power

前端 未结 8 1954
傲寒
傲寒 2020-12-17 18:02

How can I raise the numbers in list to a certain power?

8条回答
  •  我在风中等你
    2020-12-17 18:27

    def turn_to_power(list, power=1): 
        return [number**power for number in list]
    

    Example:

       list = [1,2,3]
       turn_to_power(list)
    => [1, 2, 3]
       turn_to_power(list,2)
    => [1, 4, 9]
    

    UPD: you should also consider reading about pow(x,y) function of math lib: https://docs.python.org/3.4/library/math.html

提交回复
热议问题