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

前端 未结 4 1081
北荒
北荒 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:00

    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
    
    0 讨论(0)
  • 2021-02-13 23:01

    The missing underscore makes plist a public property. I don't think that's what you want, since it does not encapsulate the functionality and you could call a.plist.append instead of a.addPerson.

    class Example():
       ...
       def filter(self, criteria):
           for p in self.plist:
               if criteria(p):
                   yield p
    
       def getByNum(self, num):
            return self.filter(lambda p: p.num == num)
    
    dave = next(a.getByNum(123))
    

    If the numbers are unique, you may also consider using a dictionary that maps from number to name or person instead of a list. But that's up to your implementation.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-02-13 23:18

    The terminology you need is 'map' or 'dictionnary' : this will lead you to the right page in the python doc.

    Extremely basic example:

    >>> a = {123:'dave', 345:'mike'}
    >>> a[123]
    'dave'
    
    0 讨论(0)
提交回复
热议问题