how to select an object from a list of objects by its attribute in python

前端 未结 4 1082
北荒
北荒 2021-02-13 22:29

Apologies if this question has already been asked but I do not think I know the correct terminology to search for an appropriate solution through google.

I would like to

4条回答
  •  执念已碎
    2021-02-13 23:03

    If those nom's are unique keys, and all you are ever going to do is access your persons using this unique key you should indeed rather use a dictionary.

    However if you want to add more attributes over time and if you like to be able to retrieve one or more person by any of those attributes, you might want to go with a more complex solution:

    class Example():
        def __init__(self):
            self.__pList = []
        def addPerson(self,name,number):
            self.__pList.append(Person(name,number))
        def findPerson(self, **kwargs):
            return next(self.__iterPerson(**kwargs))
        def allPersons(self, **kwargs):
            return list(self.__iterPerson(**kwargs))
        def __iterPerson(self, **kwargs):
            return (person for person in self.__pList if person.match(**kwargs))
    
    class Person():
        def __init__(self,name,number):
            self.nom = name
            self.num = number
        def __repr__(self):
            return "Person('%s', %d)" % (self.nom, self.num) 
        def match(self, **kwargs):
            return all(getattr(self, key) == val for (key, val) in kwargs.items())
    

    So let's assume we got one Mike and two Dave's

    a = Example()
    a.addPerson('dave',123)
    a.addPerson('mike',345)
    a.addPerson('dave',678)
    

    Now you can find persons by number:

    >>> a.findPerson(num=345)
    Person('mike', 345)
    

    Or by name:

    >>> a.allPersons(nom='dave')
    [Person('dave', 123), Person('dave', 678)]
    

    Or both:

    >>> a.findPerson(nom='dave', num=123)
    Person('dave', 123)
    

提交回复
热议问题