如何从对象列表中提取特定属性列表? [英] How to extract from a list of objects a list of specific attribute?
本文介绍了如何从对象列表中提取特定属性列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象列表.对象有 3 个字符串属性.我想制作一个仅包含类中特定属性的列表.
I have a list of objects. Object has 3 string attributes. I want to make a list containing only a specific attribute from class.
是否有任何内置函数可以做到这一点?
Is there any built-in functions to do that?
推荐答案
列表推导式可以很好地工作:
A list comprehension would work just fine:
[o.my_attr for o in my_list]
但是有内置函数的组合,因为你问:-)
But there is a combination of built-in functions, since you ask :-)
from operator import attrgetter
map(attrgetter('my_attr'), my_list)
这篇关于如何从对象列表中提取特定属性列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文