我不知道我想做的事是否是非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')])
哦,很好,谢谢,我已经从这个问题中学到了很多东西。
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] 删除。
我来说两句