Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 查找元组中与条件匹配的所有元素_Python_Function - Fatal编程技术网

Python 查找元组中与条件匹配的所有元素

Python 查找元组中与条件匹配的所有元素,python,function,Python,Function,这是我的职责: data = (130, 150, 200, 100, 130, 147) def find_elevated_bloodpressure(bloodpressure, upper_limit): for x in bloodpressure: if x > upper_limit: return x 这只返回列表中的第一个,但我希望它返回所有的 find_elevated_bloodpressure(data, 140)

这是我的职责:

data = (130, 150, 200, 100, 130, 147)

def find_elevated_bloodpressure(bloodpressure, upper_limit):
    for x in bloodpressure:
        if x > upper_limit:
            return x
这只返回列表中的第一个,但我希望它返回所有的

find_elevated_bloodpressure(data, 140)

如何获取与条件
x>上限
匹配的所有元素的列表?

当调用
return
时,函数将获取传递给
return
的所有元素,并将其作为其唯一值返回。如果需要满足条件的所有元素,可以执行以下操作,如返回列表:

return [x for x in bloodpressure if x > upper_limit]
或者将其用作发电机

#return x <-- replace with yield
yield x

#return x您需要将结果构建到一个列表中,然后返回该列表。大概是这样的:

data = (130, 150, 200, 100, 130, 147)

def find_elevated_bloodpressure(bloodpressure, upper_limit):
    results = []
    for x in bloodpressure:
        if x > upper_limit:
            results.append(x)
    return results

好吧,如果你的帖子里有问题的话……你的功能应该是什么?您的输入和输出是什么?在函数中发布问题之前,下定决心并提出一个明确的问题,当遇到
return
语句时,函数实际上就结束了。因此,当您说
return x
时,您的函数就启动了。在您的情况下,您需要返回一个大于
上限的值列表
>,我相信这是一个愚蠢的问题