如果python中的多个属性相同,则对对象进行分组
在另一个.py文件中如果python中的多个属性相同,则对对象进行分组,python,Python,在另一个.py文件中 def class ObjClass: attr1; attr2; attr3; attr4; attr5; def __init__(self): print("Some logic goes here") 现在,对于列表中的所有对象,我想对具有相同attr1、attr2、attr3但不同attr4和attr5的对象进行分组。我该怎么做 什么是蟒蛇式的方法。我知道如何使用列表,但不使用对象 obj1 = o
def class ObjClass:
attr1;
attr2;
attr3;
attr4;
attr5;
def __init__(self):
print("Some logic goes here")
现在,对于列表中的所有对象,我想对具有相同attr1、attr2、attr3但不同attr4和attr5的对象进行分组。我该怎么做
什么是蟒蛇式的方法。我知道如何使用列表,但不使用对象
obj1 = obj.objClass();
obj1.attr1 = foo
obj1.attr2 = bar (..... so on and so forth)
objList = []
objList.append(obj1)
objList.append(obj2)
objList.append(obj3)
objList.append(obj4)
objList.append(obj5)
返回对象列表-选择的属性等于所需的值 我想将具有相同属性的对象分组。(不将属性与某个值进行比较)。如果我之前的描述不清楚,我深表歉意。我修改了它。改为使用
hasttr
。如果对所有感兴趣的属性运行此操作,则可以对列表执行集合加减运算,以找到要查找的集合。
def grouper(objs, attr_name):
group =[]
for obj in objs:
if hasattr(obj, attr_name):
group.append(obj)
return group