简体   繁体   English

如何通过python中的属性访问特定的类实例?

[英]How to access a specific class instance by attribute in python?

Say I have a class Box with two attributes, self.contents and self.number. 假设我有一个具有两个属性的类Box,它们是self.contents和self.number。 I have instances of box in a list called Boxes. 我在名为“框”的列表中有框的实例。 Is there anyway to access/modify a specific instance by its attribute rather than iterating through Boxes? 是否有通过属性访问/修改特定实例的方法,而不是通过Box进行迭代? For example, if I want a box with box.number = 40 (and the list is not sorted) what would be the best way to modify its contents. 例如,如果我想要一个box.number = 40的框(并且列表未排序),那么修改其内容的最佳方法是什么。

If you need to do it more frequently and you have unique number s, then create a dictionary: 如果您需要更频繁地执行此操作并且您拥有唯一的number s,那么可以创建一个字典:

numberedBox = dict((b.number, b) for b in Boxes)

you can then access your boxes directly with numbers: 然后,您可以直接使用数字访问框:

numberedBox[40]

but if you want to change their number, you will have to modify the numberedBox dictionary too... 但是如果您想更改其编号,则也必须修改numberedBox词典...

Otherwise yes, you have to iterate over the list. 否则,您必须遍历该列表。

The most straightforward way is to use a list comprehension: 最直接的方法是使用列表理解:

answer=[box for box in boxes if box.number==40]

Be warned though. 但是被警告。 This actually does iterate over the whole list . 实际上,这确实会遍历整个list Since the list is not sorted, there is no faster method than to iterate over it (and thus do a linear search), unless you want to copy all the data into some other data structure (eg dict , set or sort the list ). 由于list没有排序,除非您想将所有数据复制到其他数据结构(例如dictset或sort list )中,否则没有比迭代它更快的方法(因此可以进行线性搜索)。

使用内置的过滤器:

wanted_boxes = filter(lambda box: box.number == 40, boxes)

Although not as flexible as using a dictionary, you might be able to get by using a simple lookup table to the map box numbers to a particular box in boxes . 虽然不够灵活使用字典,您可能能够通过使用简单的查找表的映射盒号码在一个特定的盒子拿到boxes For example if you knew the box numbers could range 0...MAX_BOX_NUMBER , then the following would be very fast. 例如,如果您知道框号的范围是0...MAX_BOX_NUMBER ,那么以下操作将非常快。 It requires only one full scan of the Boxes list to setup the table. 只需对“ Boxes列表进行一次完整扫描即可设置该表。

MAX_BOX_NUMBER = ...

# setup lookup table   
box_number = [None for i in xrange(MAX_BOX_NUMBER+1)]
for i in xrange(len(Boxes)):
    box_number[Boxes[i].number] = Boxes[i]

box_number[42] # box in Boxes with given number (or None)

If the box numbers are in some other arbitrary range, some minor arithmetic would have to be applied to them before their use as indices. 如果框号在其他任意范围内,则在将它们用作索引之前,必须对其应用一些较小的运算。 If the range is very large, but sparsely populated, dictionaries would be the way to go to save memory but would require more computation -- the usual trade-off. 如果范围很大,但人烟稀少,那么字典将是节省内存的方法,但需要更多的计算,这是通常的折衷方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查具有特定属性的类的实例是否存在 - How to check the existence of an instance of a class with a specific attribute 如何将属性添加到 python class 中的实例变量 - How to add attribute to instance variables in a python class python,如何修改类实例的dict属性 - python, how to modify a dict attribute of a class instance 如何扫描一个class的实例返回特定属性值最高的实例名PYTHON - How to scan instances of a class to return the instance name with the highest specific attribute value PYTHON 为什么我无法在Python中访问instance .__ class__属性? - Why can't I access the instance.__class__ attribute in Python? 如何访问具有特定 Class 属性值的 Class 实例? - How can I access a Class instance with a certain Class attribute value? 如何在Python中附加类的实例,然后提取实例的属性? - How to append an instance of a class and then pull out an attribute of an instance in Python? Python:如何确定属性(按名称)是类还是实例属性? - Python: How determine if attribute (by name) is class or instance attribute? 如何将类属性更改为实例属性Python编程 - How to change a class attribute into an instance attribute Python Programming 如何访问 Python 中 class 级别的实例变量? - how to access the instance variables at class level in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM