Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

oop

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 select an object from a list of objects by the value of it's attribute, for example:

class Example():
    def __init__(self):
        self.pList = []
    def addPerson(self,name,number):
        self.pList.append(Person(self,name,number))

class Person():
    def __init__(self,name,number):
        self.nom = name
        self.num = number


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

a.pList #.... somehow select dave by giving the value 123

in my case the number will always be unique

Thanks for the help

like image 847
Anake Avatar asked Mar 03 '11 11:03

Anake


2 Answers

Try

dave = next(person for person in a.pList if person.num == 123)

or

for person in a.pList:
    if person.num == 123:
        break
else:
    print "Not found."
dave = person
like image 164
Sven Marnach Avatar answered Oct 02 '22 20:10

Sven Marnach


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)
like image 23
codecraft Avatar answered Oct 02 '22 21:10

codecraft