提供列表中的Square的Python函数 [英] Python function for Square in a provided list
本文介绍了提供列表中的Square的Python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像 [2,3,4,5]
这样的列表,我想使用python函数将这些数字平方
尝试过以下Python代码
def正方形(列表= []):对于清单中的我:new_value = []new_value.append(i ** 2)返回new_value
我得到列表中唯一第一个条目的平方.
square(2,3,4)
解决方案
列表理解使此功能成为一个单一的衬里并且更加清晰.
def平方(x):返回[x中y的y ** 2]
I have a list like [2,3,4,5]
and i want square of these number using python function
Have tried below Python code
def square(list=[]):
for i in list:
new_value=[]
new_value.append(i**2)
return new_value
I get the square of only first entry in a list.
square(2,3,4)
解决方案
List comprehension makes this function a single liner and much clearer.
def square(x):
return [y**2 for y in x]
这篇关于提供列表中的Square的Python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文