Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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中使用lambda的按位或列表元素_Python_Python 2.7 - Fatal编程技术网

python中使用lambda的按位或列表元素

python中使用lambda的按位或列表元素,python,python-2.7,Python,Python 2.7,我可以这样使用lambda函数按位或列表中的所有元素吗 lst = [1, 1, 1] f = lambda x: x | b for b in lst 当我这样做时,我得到一个语法错误您想要的: reduce接受一个二进制函数和一个iterable,并在从第一对开始的所有元素之间应用运算符。 注意:在Python3中,它移动到模块 您也可以使用操作符模块中的函数,而不是自己编写lambda: from operator import or_ f = reduce(or_, lst) fro

我可以这样使用lambda函数按位或列表中的所有元素吗

lst = [1, 1, 1]
f = lambda x: x | b for b in lst
当我这样做时,我得到一个
语法错误

您想要的:

reduce
接受一个二进制函数和一个iterable,并在从第一对开始的所有元素之间应用运算符。 注意:在Python3中,它移动到模块

您也可以使用
操作符
模块中的函数,而不是自己编写lambda:

from operator import or_
f = reduce(or_, lst)
from operator import or_
f = reduce(or_, lst)