如何在Python中的列表中存储特定属性的实例值? [英] How do I store instance values of a particular attribute in a list in Python?
本文介绍了如何在Python中的列表中存储特定属性的实例值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是python的新手。我正在学习OOP。我的问题是:
如何在列表中存储特定属性的实例值?例如,值name属性应该作为['table','chair']存储在列表中
我尝试过:
i am a novice in python. I am learning OOP. My question is :
How do i store instance values of a particular attribute in a list? for example the values name attribute should be stored in a list as ['table','chair']
What I have tried:
class retail:
def __init__(self,name,price):
self.name=name
self.price=price
item1=retail('table',100)
item2=retail('chair',200)
推荐答案
这样做,但如果你有很多对象,它效率不高。
This will do it, but it is not very efficient if you have many objects.
[x.name for x in [item1, item2]]
也许你可以提供你想要达到的确切内容的更多细节。
Maybe you could provide more details of exactly what you are trying to achieve.
这篇关于如何在Python中的列表中存储特定属性的实例值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文