python - convert a list of objects to a list of a data member -
say have list of objects. object has data member 'name'. want sub list of objects have value of 'name'. elegant way beyond:
class person(base): name = column(text) p1 = person(name="joe") p2 = person(name="jill") plst = [ p1, p2 ] name_test = "jill" found_people = list() person in plst: if person.name == name_test: found_people.append(person)
looking elegant solution not verbose. not sure if python code compiles or not :)
you use list comprehension.
class person(base): name = column(text) plist = [person(name="joe"), person(name="jill")] found_people = [person person in plist if person.name == "jill"]
Comments
Post a Comment