Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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/blackberry/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 - Fatal编程技术网

如何查找列表中是否有函数(Python)

如何查找列表中是否有函数(Python),python,Python,我有一个保存但未运行的函数列表。 我需要遍历这个列表,看看列表中是否存在我要查找的项目之一 我该怎么做 下面是函数的存储方式 self.pysat_fun.set_fun_list(self.pysat_fun.set_file_outpath) # add this function to the pysat list to be run def set_fun_list(self, fun, replacelast=False): if replacelast:

我有一个保存但未运行的函数列表。
我需要遍历这个列表,看看列表中是否存在我要查找的项目之一

我该怎么做

下面是函数的存储方式

self.pysat_fun.set_fun_list(self.pysat_fun.set_file_outpath)  # add this function to the pysat list to be run

def set_fun_list(self, fun, replacelast=False):
    if replacelast:
        self.fun_list[-1] = fun
    else:
        self.fun_list.append(fun)
下面您可以看到列表中包含的内容。

这是我的尝试

    for i in range(0, len(self.fun_list)):
        if pysat_func.do_norm in self.fun_list:
            print("True")
或者,简单地说:

print (pysat_func.do_norm in self.fun_list [self.leftOff : ])
其工作原理如下:

self.fun_list
要在其中搜索函数的列表

self.fun_list [self.leftOff : ]
是该列表的尾部,因为从原始代码来看 您只想从索引为self.leftOff的元素中搜索

[self.leftOff : ]

被称为切片,后面的空白:意味着“直到结束”。

pysat_func.do_norm in aList
是一个布尔表达式,其计算结果为
True
当且仅当
pysat\u func.do\u norm
位于
aList

所以

是一个布尔表达式,其计算结果为
True
当且仅当
pysat\u func.do\u norm
在我所说的尾部

print (anyThing)
将在其大括号内打印任何内容,这恰好是上面提到的
True
(或
False

以下示例说明哪些绑定方法在特定列表中,哪些不在特定列表中:

class A:
    def f (self):
        print ('f')

    def g (self):
        print ('g')

a1 = A ()
a2 = A () 

aList = [a1.f, a2.g]

print (
    a1.f in aList,
    a1.g in aList,
    a2.f in aList,
    a2.g in aList
)
印刷品:

True False False True

奇怪的当我使用你的打印功能打印出来是假的打印需要在for循环之外,对吗<代码>打印(self.fun\u列表[self.leftOff:]中的pysat\u func.do\u norm)由于您只查找一个函数(如果我理解正确),因此第一个代码块中的打印位置正确。如果找到函数,则中断将跳出循环。第二个代码块应该与第一个代码块等效。您确定您确实在存储绑定函数吗?所以对象a.do_范数和对象b.do_范数不同?看看你提出的一个老问题,你不是在储存lambda的吗?可能会显示一些上下文代码。请让我知道这是否足够,并且您的搜索循环是正确的,但是如果没有
中断
,您将继续搜索列表到最后,即使您已经找到了函数。在
print(self.fun\u列表中的pysat\u func.do\u norm[self.leftOff:])
中,
print
不在任何循环之外,不需要循环。但正如前面所说,您的搜索循环也是正确的,尽管有点冗长。
class A:
    def f (self):
        print ('f')

    def g (self):
        print ('g')

a1 = A ()
a2 = A () 

aList = [a1.f, a2.g]

print (
    a1.f in aList,
    a1.g in aList,
    a2.f in aList,
    a2.g in aList
)
True False False True