简体   繁体   English

按对象属性访问Python对象列表

[英]Accessing list of Python objects by object attribute

I don't know if what I am trying to do is so un-Pythonic that I'm simply trying to do it wrong, or if I don't know how to ask the question properly. 我不知道我试图做的是不是Pythonic,我只是试图做错,或者我不知道如何正确地提出这个问题。 It makes sense to me to be able to do this, but I have searched 15 different ways and can't find the answer. 能够做到这一点对我来说是有意义的,但我已经搜索了15种不同的方法,但找不到答案。

What I want to do seems so simple: I have a list of objects. 我想做的事情看起来很简单:我有一个对象列表。 I want to access that list by a property of the objects. 我想通过对象的属性访问该列表。 This code works: 此代码有效:

class Fruit:
    def __init__(self, name, color):
        self.name = name
        self.color = color

    def __str__(self):
        return self.name    

class BaseballPlayer:
    def __init__(self, name, number, position):
        self.name = name
        self.number = number
        self.position = position

    def __str__(self):
        return self.name    

class ListByProperty(list):
    def __init__(self, property, list):
        super(ListByProperty, self).__init__(list)
        self.property = property

    def __getitem__(self, key):
        return [item for item in self if getattr(item, self.property) == key][0]

fruits = ListByProperty('name', [Fruit('apple', 'red'), Fruit('banana', 'yellow')])

baseballTeam = ListByProperty('position', [BaseballPlayer('Greg Maddux', 31, 'P'),
                                           BaseballPlayer('Javy Lopez', 8, 'C')])
teamByNumber = ListByProperty('number', baseballTeam)

print 'Apples are', fruits['apple'].color

pitcher = baseballTeam['P']
print 'The pitcher is #%s, %s' % (pitcher.number, pitcher)
print '#8 is', teamByNumber[8]

>>> Apples are red
The pitcher is #31, Greg Maddux
#8 is Javy Lopez

But do I really have to make my own list class to do something this simple? 但是,我真的必须让我自己的列表类做这么简单的事情吗? Is there no generic way other than looping or a listcomp? 除了循环或listcomp之外,没有通用的方法吗? This seems like it should be a very common thing to do, to have a list of objects and access items in the list by a property of the objects. 这似乎应该是一个非常常见的事情,通过对象的属性获得列表中的对象和访问项。 It seems like it should be commonly supported in a way similar to sorted(key=...) . 它似乎应该以类似于sorted(key=...)的方式得到普遍支持。

Note that this is not the same case as needing a dict. 请注意,这与需要dict的情况不同。 In fact, the whole point of using a list of objects instead of a dict is to avoid having to do something like: 实际上,使用对象列表而不是字典的全部意义在于避免必须执行以下操作:

fruits = {'apple': Fruit('apple', 'red')}

...which requires you to type apple twice. ...这需要你输入两次apple It seems like there should be a generic way to do something like this: 看起来应该有一种通用的方法来做这样的事情:

print 'Apples are', fruits['apple'].color

...without having to subclass list . ...无需子类list

And okay, you can build a dict like this: 好吧,你可以建立一个像这样的字典:

fruits = [Fruit('apple', 'red'), Fruit('banana', 'yellow')]
fruits = {f.name: f for f in fruits}

Or you can one-line it, but that still seems...uh...syntactically sour? 或者你可以单行,但看起来仍然......呃......语法上是酸的? :) :)

The best way I've figured out so far is: 我到目前为止最好的解决方法是:

class DictByProperty(dict):
    def __init__(self, property, list):
        super(DictByProperty, self).__init__({getattr(i, property): i for i in list})
        self.property = property

fruits = DictByProperty('name', [Fruit('apple', 'red')])

Oh well, thanks, I've learned a lot already from this question. 哦,谢谢,我已经从这个问题中学到了很多东西。

class Fruit:
    def __init__(self, name, color):
        self.name = name
        self.color = color

fruits = dict(zip(['apple', 'banana'], [Fruit('apple', 'red'), Fruit('banana', 'yellow')]))

print("An apple is %s" % fruits['apple'].color)

OR: 要么:

fruits = {fruit.name : fruit for fruit in [Fruit('apple', 'red'), Fruit('banana', 'yellow')]}

print("An apple is %s" % fruits['apple'].color)

The following does infact produce a set: 以下确实产生了一组:

fruits = {Fruit('apple', 'red'), Fruit('banana', 'yellow')}

Note the difference from the way I created the dict 注意与我创建dict的方式的区别

fruits = [Fruit('apple', 'red'), Fruit('banana', 'yellow')]

print("An apple is %s" % fruits[fruits.index('apple')].color)

Doesn't work because your list contains Objects of type fruit not strings, and that is the same story here: 不起作用,因为你的列表包含水果类型的对象而不是字符串,这是相同的故事:

fruits = FruitList([Fruit('apple', 'red'), Fruit('banana', 'yellow')])

print("An apple is %s" % fruits['apple'].color)

To make the above method work, do the following: 要使上述方法有效,请执行以下操作:

class Fruit:
    def __init__(self, name, color):
        self.name = name
        self.color = color

    def __eq__(self, other):
        if isinstance(other, Fruit):
            return (self.name, self.color) == (other.name, other.color)
        return self.name == other

fruits = [Fruit('apple', 'red'), Fruit('banana', 'yellow')]
print("An apple is %s" % fruits[fruits.index('apple')].color)

I don't recommend this because in the case that your list happens to contain the string apple , then attribute call to color becomes invalid because strings do not have an attribute called color 我不推荐这个,因为在你的列表恰好包含字符串apple的情况下,对color属性调用变得无效,因为字符串没有名为color的属性

You could create an instance. 您可以创建一个实例。

apple = Fruit('apple', 'red')
print(apple.color) # I use Python 3.x

I'm not sure I'm following your question. 我不确定我是在关注你的问题。 But maybe this helps. 但也许这会有所帮助。

edit: in an attempt to gain my reputation back... 编辑:试图获得我的声誉...

you could use instances WITHIN a dictionary. 你可以在字典中使用WITHIN实例。 For example... 例如...

banana = Fruit('banana', 'yellow')
apple = Fruit('apple', 'red')
fruits = {'apple':apple, 'banana':banana}

or if you prefer: 或者如果您愿意:

fruits = {'apple':Fruit('apple', 'red'), 'banana':Fruit('banana', 'yellow')}

either way, you can call a fruit's color simply with 无论哪种方式,您都可以简单地调用水果的颜色

print(fruits['banana'].color)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM