将属性添加到类列表以返回具有特定属性的所有对象

时间:2014-06-20 11:42:34

标签: python list class filter attributes

我有以下问题:

让我们设置场景!

假设我有一个具有一些基本属性的人类:

class Person(object):
    def __init__(self, name=None, gender=None, single=None):
        self.name=name
        self.gender=gender
        self.single=single

我创建了一个名为Dating的列表类,它将包含所有Person对象

class Dating(object):
    def __init__(self):
        self.members=[]

My_People=Dating()

My_People.members.append(Person("Jack","Male",False))
My_People.members.append(Person("Jill","Female",True))
My_People.members.append(Person("George","Male",True))
My_People.members.append(Person("Sandy","Female",False))

是的,那么问题是什么?

是否可以通过创建某种属性来为列表类分配属性以访问My_People列表的单个成员:

My_People.members.singles

这样会返回一个包含 single == True 属性的Person对象列表?

感谢所有帮助。 (顺便说一句,我以前很少有Python经验)

4 个答案:

答案 0 :(得分:3)

要添加这样的属性,您必须子类列表类型:

class FilterableList(list):
    def __getattr__(self, name):
        # assume non-existing attributes are boolean filters
        return [elem for elem in self if getattr(elem, name)]

请注意,这并非灵活;这种方式只能找到True个值。您可以为属性名称赋予更多含义,但通常您仍希望在Dating类上执行此操作,并使用方法更明确地过滤数据

演示:

>>> class Person(object):
...     def __init__(self, name, gender, single):
...         self.name=name
...         self.gender=gender
...         self.single=single
...     def __repr__(self):
...         return 'Person({name!r}, {gender!r}, {single!r})'.format(**vars(self))
... 
>>> class FilterableList(list):
...     def __getattr__(self, name):
...         # assume non-existing attributes are boolean filters
...         return [elem for elem in self if getattr(elem, name)]
... 
>>> members = FilterableList([Person("Jack","Male",False), Person("Jill","Female",True), Person("George","Male",True), Person("Sandy","Female",False)])
>>> members
[Person('Jack', 'Male', False), Person('Jill', 'Female', True), Person('George', 'Male', True), Person('Sandy', 'Female', False)]
>>> members.single
[Person('Jill', 'Female', True), Person('George', 'Male', True)]

答案 1 :(得分:3)

首先,您不需要在__init__()实例上调用My_People,Python会自动执行此操作。

对于这个问题,您可以从list继承并添加singles属性,如下所示:

class Person(object):
    def __init__(self, name=None, gender=None, single=None):
        self.name=name
        self.gender=gender
        self.single=single

class Dating(list):
    @property
    def singles(self):
        return [person for person in self if person.single ]

My_People=Dating()

My_People.append(Person("Jack","Male",False))
My_People.append(Person("Jill","Female",True))
My_People.append(Person("George","Male",True))
My_People.append(Person("Sandy","Female",False))

print My_People.singles

答案 2 :(得分:1)

怎么样:

class Attribute(list):
    def __getattr__(self, attr):
        if attr == 'single':
            return [person for person in self if person.single]
        raise AttributeError()

class Person(object):
    def __init__(self, name=None, gender=None, single=None):
        self.name = name
        self.gender = gender
        self.single = single

    def __repr__(self):
        return 'Person({person.name}, {person.gender}, {person.single})'.format(person=self)


class Dating(object):
    members = Attribute()


My_People = Dating()

My_People.members.append(Person("Jack", "Male", False))
My_People.members.append(Person("Jill", "Female", True))
My_People.members.append(Person("George", "Male", True))
My_People.members.append(Person("Sandy", "Female", False))

>>> print My_People.members.single
[Person(Jill, Female, True), Person(George, Male, True)]
>>> print My_People.members
[Person(Jack, Male, False), Person(Jill, Female, True), Person(George, Male, True), Person(Sandy, Female, False)]

答案 3 :(得分:0)

如果您不依赖于My_People.members.singles语法,则可以更简单地执行此操作:

class Dating(object):

    def __init__(self):
        self.members = []

    @property
    def single_members(self):
        return [m for m in members if m.single]

现在您使用以下命令访问列表:

My_People.single_members