Group objects if multiple attributes are same in python -


def class objclass:     attr1;     attr2;     attr3;     attr4;     attr5;      def __init__(self):        print("some logic goes here") 

in .py file

obj1 = obj.objclass(); obj1.attr1 = foo obj1.attr2 = bar   (..... on , forth)  objlist = [] objlist.append(obj1) objlist.append(obj2) objlist.append(obj3) objlist.append(obj4) objlist.append(obj5) 

now objects there in list, want group ones have same attr1, attr2, attr3 different attr4 , attr5. how do this?

what pythonic way of doing this. know how using lists not using objects.

def grouper(objs, attr_name):     group =[]     obj in objs:         if hasattr(obj, attr_name):             group.append(obj)     return group 

returns list of objects- attribute select equal value want.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -