Python 平方函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17499268/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:21:00  来源:igfitidea点击:

Python Squares Function

pythonpython-3.x

提问by user2553807

I'm writing a function that will return a list of square numbers but will return an empty list if the function takes the parameter ('apple') or (range(10)) or a list. I have the first part done but can't figure out how to return the empty set if the parameter n is not an integer- I keep getting an error: unorderable types: str() > int() I understand that a string can't be compared to an integer but I need it to return the empty list.

我正在编写一个函数,该函数将返回一个平方数列表,但如果该函数采用参数 ('apple') 或 (range(10)) 或一个列表,则将返回一个空列表。我已经完成了第一部分,但是如果参数 n 不是整数,我无法弄清楚如何返回空集 - 我不断收到错误:无法排序的类型:str() > int() 我知道字符串可以' t 与整数进行比较,但我需要它来返回空列表。

def square(n):

    return n**2

def Squares(n):

    if n>0:
        mapResult=map(square,range(1,n+1))
        squareList=(list(mapResult))   
    else:
        squareList=[]

    return squareList

采纳答案by Matias Grioni

You can use the typefunction in python to check for what data type a variable is. To do this you would use type(n) is intto check if nis the data type you want. Also, mapalready returns a list so there is no need for the cast. Therefore...

您可以使用typepython 中的函数来检查变量是什么数据类型。为此,您将使用type(n) is int检查是否n是您想要的数据类型。此外,map已经返回一个列表,因此不需要演员表。所以...

def Squares(n):
    squareList = []

    if type(n) is int and n > 0:
        squareList = map(square, range(1, n+1))

    return squareList

回答by TerryA

You can not compare strings to integers as you have tried to do. If you want to check if nis an integer or not, you can use isinstance():

您无法像尝试那样将字符串与整数进行比较。如果要检查是否n为整数,可以使用isinstance()

def squares(n):
    squareList = []
    if isinstance(n, (int, float)) and n > 0: # If n is an integer or a float
        squareList = list(map(square,range(1,n+1)))     
    return squareList

Now, if a string or a list is given as an argument, the function will immediately return an empty list []. If not, then it will continue to run normally.

现在,如果将字符串或列表作为参数给出,该函数将立即返回一个空列表[]。如果没有,那么它将继续正常运行。



Some examples:

一些例子:

print(squares('apple'))
print(squares(5))
print(squares(range(10)))

Will return:

将返回:

[]
[1, 4, 9, 16, 25]
[]

回答by HennyH

You can chain all the conditions which result in returning an empty list into one conditional using or's. i.e if it is a list, or equals 'apple', or equals range(10)or n < 0then return an empty list. ELSE return the mapResult.

or如果它是一个列表,则可以使用'sie将导致返回空列表的所有条件链接到一个条件中,或者等于'apple'或等于,range(10)或者n < 0然后返回一个空列表。否则返回 mapResult。

def square(n):
    return n**2

def squares(n):
   if isinstance(n,list) or n == 'apple' or n == range(10) or n < 0:
      return []
   else:
      return list(map(square,range(1,n+1)))

isinstancechecks if nis an instance of list.

isinstance检查是否n是 的实例list

Some test cases:

一些测试用例:

print squares([1,2])
print squares('apple')
print squares(range(10))
print squares(0)
print squares(5)

Gets

获取

[]
[]
[]
[]
[1, 4, 9, 16, 25]
>>>