通过对象属性访问Python对象列表

用户名

我不知道我想做的事是否是非Pythonic的,以至于我只是试图做错事情,或者我不知道如何正确地提出问题。能够做到这一点对我来说很有意义,但是我已经搜索了15种不同的方法,但找不到答案。

我想做的事情看起来很简单:我有一个对象列表。我想通过对象的属性访问该列表。此代码有效:

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

但是,是否真的需要创建自己的列表类才能做到这一点呢?除了循环或listcomp之外,没有其他通用方法吗?拥有对象列表并通过对象的属性访问列表中的项目似乎是一件很普通的事情。似乎应该以类似的方式普遍支持它sorted(key=...)

请注意,这与需要命令不同。实际上,使用对象列表而不是字典的全部目的是避免必须执行以下操作:

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

...这需要您输入apple两次。似乎应该有一种通用的方式来做这样的事情:

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

...无需继承list

好的,您可以构建一个像这样的字典:

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

或者,您也可以单行,但是看起来...呃...语法上还是很酸吗?:)

到目前为止,我发现的最好方法是:

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')])

哦,很好,谢谢,我已经从这个问题中学到了很多东西。

smac89
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)

或者:

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

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

实际上,以下内容会产生一个集合:

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

注意与我创建字典的方式不同

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

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

不起作用,因为您的列表包含水果类型而不是字符串类型的对象,并且这里的情况与此相同:

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

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

要使上述方法起作用,请执行以下操作:

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)

我不建议这样做,因为在您的列表碰巧包含字符串的情况下,由于字符串没有名为的属性apple,因此对属性的调用color变得无效color

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过索引访问python列表对象

来自分类Dev

通过索引访问python列表对象

来自分类Dev

在python中访问对象列表的属性

来自分类Dev

访问列表中对象的属性

来自分类Dev

通过对象属性获取列表中的对象

来自分类Dev

在Python中通过列表全面搜索访问对象

来自分类Dev

通过名称访问动态对象属性

来自分类Dev

通过属性名称数组访问嵌套对象

来自分类Dev

通过UnaryExpression访问对象属性值

来自分类Dev

通过对象属性访问JSON数组

来自分类Dev

通过直接字段访问复制对象属性

来自分类Dev

通过分配访问对象属性

来自分类Dev

通过this和ObjectName访问对象属性

来自分类Dev

通过对象属性访问JSON数组

来自分类Dev

Javascript通过其属性访问主对象

来自分类Dev

Python属性访问导致属性对象本身

来自分类Dev

通过其DateTime属性订购对象列表

来自分类Dev

Python - 如何通过查找对象的属性来搜索存储在列表中的对象

来自分类Dev

我如何访问给定索引的对象列表中的对象属性?

来自分类Dev

如何访问给定索引的对象列表中的对象属性?

来自分类Dev

嵌套对象的对象访问属性

来自分类Dev

列表中的对象需要访问父列表属性

来自分类Dev

如何从对象列表访问对象

来自分类Dev

Python:从回调访问对象属性

来自分类Dev

Python子对象未访问父属性

来自分类Dev

无法使用Python访问对象内的列表

来自分类Dev

如何访问列表中的特定对象(python)

来自分类Dev

访问类对象中的列表,Python

来自分类Dev

Python:从存储在字典中的对象访问对象属性