Raising elements of a list to a power

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

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

8条回答
  •  猫巷女王i
    2020-12-17 18:21

    Actually your script do what you want, and that is the result (keep in mind that you are applying the power function to [1,2,3,4,5,6,7,8,9])

    ('Cubic list:', [1, 8, 27, 64, 125, 216, 343, 512, 729])

    Your problem is that you also modified the original list, due to the nature on the list type in python. If you want to keep also your original list you should pass a copy of it to your function. You can do it like this

    def main():
        numbers=[1,2,3,4,5,6,7,8,9]
        numbers3=power(numbers[:])
        print('Original list:', numbers)
        print('Cubic list:', numbers3)
    

提交回复
热议问题