Create list of object attributes in python

前端 未结 4 1484
长情又很酷
长情又很酷 2021-01-11 12:26

I have a list of objects:

[Object_1, Object_2, Object_3]

Each object has an attribute: time:

Object_1.time = 20
Object_2.ti         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-11 12:39

    List comprehension is what you're after:

    list_of_objects = [Object_1, Object_2, Object_3]
    [x.time for x in list_of_objects]
    

提交回复
热议问题