Raising elements of a list to a power

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

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

相关标签:
8条回答
  • 2020-12-17 18:43

    You can simply do:

    numbers=[1,2,3,4,5,6,7,8,9]
    numbers3 = []
    for n in numbers:
        numbers3.append(n**3)
    
    print('Original list:', numbers)
    print('Cubic list:', numbers3)
    
    0 讨论(0)
  • 2020-12-17 18:48

    Another map pattern, using lambda instead of function.partial():

    numbers=[1,2,3,4]
    squares=list(map(lambda x:pow(x,2),numbers))
    print(squares)
    
    0 讨论(0)
提交回复
热议问题