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

前端 未结 4 1094
北荒
北荒 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
    

提交回复
热议问题