Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/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_List_Types_Comparison_Comparison Operators - Fatal编程技术网

Python 如何查看列表中是否存在某个类

Python 如何查看列表中是否存在某个类,python,list,types,comparison,comparison-operators,Python,List,Types,Comparison,Comparison Operators,我有一个类,user,它有一个属性metadata 元数据是对象列表,每个对象具有不同的类,例如: user.metatada=[Employee()、Student()、OtherClass()] 在更新脚本中,我需要检查列表中是否存在某种类型,如下所示: if type(Employee()) in user.metadata: replace user.metadata[indexOfThatEmployee] with new Employee() else: user.meta

我有一个类,
user
,它有一个属性
metadata

元数据
是对象列表,每个对象具有不同的类,例如:

user.metatada=[Employee()、Student()、OtherClass()]

在更新脚本中,我需要检查列表中是否存在某种类型,如下所示:

if type(Employee()) in user.metadata:
  replace user.metadata[indexOfThatEmployee] with new Employee()
else:
  user.metadata.append(new Employee())
是否有任何方法可以轻松检查列表中是否存在某种类型的文件?

明白了

test = [Employee()]

if any(isinstance(x, Employee) for x in user.metadata):
    user.metadata = [x for x in user.metadata if not isinstance(x, Employee)] + test
else:
    user.metadata = user.metadata + test

因此,这将检查列表中是否存在作为Employee类实例的对象,如果存在,则过滤现有Employee对象的列表并添加到新的列表中,如果不存在,只需添加它

您需要检查该属性中是否存在该
,并替换它(如果存在)?基本上是的。