Python 将列表的元素提升到幂
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30323439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Raising elements of a list to a power
提问by Amelia155
How can I raise the numbers in list to a certain power?
如何将列表中的数字提高到一定的幂?
采纳答案by Jorick Spitzen
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
https://docs.python.org/3.4/tutorial/datastructures.html#list-comprehensions
回答by maggick
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)
回答by konart
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
UPD:您还应该考虑阅读有关libpow(x,y)
功能的信息math
:https: //docs.python.org/3.4/library/math.html
回答by GiovanniPi
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])
实际上你的脚本做你想做的,这就是结果(请记住,你正在将幂函数应用于 [1,2,3,4,5,6,7,8,9])
('Cubic list:', [1, 8, 27, 64, 125, 216, 343, 512, 729])
('立方表:', [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
您的问题是由于python中列表类型的性质,您还修改了原始列表。如果您还想保留原始列表,则应将其副本传递给您的函数。你可以这样做
def main():
numbers=[1,2,3,4,5,6,7,8,9]
numbers3=power(numbers[:])
print('Original list:', numbers)
print('Cubic list:', numbers3)
回答by dermen
Use a list comprehension for some increased speed:
使用列表理解来提高速度:
print('Original list:', numbers)
print('Cubic list:', [n**3 for n in numbers])
回答by tommy.carstensen
Nobody has mentioned map
and functools.partial
and the accepted answer does not mention pow
, but for the sake of completeness I am posting this solution:
没有人提到map
并且functools.partial
接受的答案没有提到pow
,但为了完整起见,我发布了这个解决方案:
import functools
bases = numbers = [1,2,3]
power = exponent = 3
cubed = list(map(functools.partial(pow, exponent), numbers))
I would use a list comprehension myself as suggested, but I think functools.partial
is a very cool function that deserves to be shared. I stole my answer from @sven-marnach hereby the way.
我会按照建议自己使用列表理解,但我认为这functools.partial
是一个非常酷的函数,值得分享。顺便说一下,我从@sven-marnach 那里偷了我的答案。
回答by Robert Calhoun
Another map
pattern, using lambda
instead of function.partial()
:
另一种map
模式,使用lambda
代替function.partial()
:
numbers=[1,2,3,4]
squares=list(map(lambda x:pow(x,2),numbers))
print(squares)