Raising elements of a list to a power

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

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

8条回答
  •  暖寄归人
    2020-12-17 18:40

    Use list comprehension:

    def power(my_list):
        return [ x**3 for x in my_list ]
    

    https://docs.python.org/3.4/tutorial/datastructures.html#list-comprehensions

提交回复
热议问题