Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python字符串和int_Python - Fatal编程技术网

Python字符串和int

Python字符串和int,python,Python,我一直在学习一门在线课程,但我遇到了一个路障。我想看看一个整数列表是否大于一个给定的整数。我不断收到错误TypeError:“>=”在“list”和“int”的实例之间不受支持。。帮忙 以下是我的尝试: def numCount(someList, comparison): returnVal = [] if numList >= comparison: returnVal += numList ret

我一直在学习一门在线课程,但我遇到了一个路障。我想看看一个整数列表是否大于一个给定的整数。我不断收到错误
TypeError:“>=”在“list”和“int”的实例之间不受支持。
。帮忙

以下是我的尝试:

    def numCount(someList, comparison):
        returnVal = []
        if numList >= comparison:
                returnVal += numList
        return returnVal

    numList=[0, 2, 4, 5, 10]
    print(numCount(someList, 9))

Python不支持将列表与int进行比较,主要是因为它没有意义。你的意思是想看看整数列表的和是否大于给定的整数?还是要查找列表中大于给定数字的所有整数,并返回包含这些整数的列表?

您必须迭代列表中的每个项目并比较它们:

def numCount(someList, comparison):
    returnVal = []
    for elt in someList:
        if elt >= comparison:
            returnVal.append(elt)
    return returnVal

numList=[0, 2, 4, 5, 10]
print(numCount(numList, 9))
输出:
[10]


输出是
numList
中大于或等于比较整数的值的列表。

numList
现在是一个列表,而
comparison
是一个int-你知道如何从列表
numList
中得到int吗?你似乎把变量弄混了,你是说
print(numCount)吗(numList,9))
如果someList>=比较:
。注意:您可以使用
filter()
,例如
返回列表(filter(lambda x:x>=比较,someList))
谢谢您,先生。作为一个初学者,这对我帮助很大。谢谢^