Find object by its member inside a List in python

后端 未结 4 1568
执笔经年
执笔经年 2021-02-02 11:21

lets assume the following simple Object:

class Mock:
    def __init__(self, name, age):
        self.name = name
        self.age = age

then I

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-02 11:45

    List comprehensions can pick these up:

    new_list = [x for x in myList if x.age == 30]
    

提交回复
热议问题