Python获取列表中具有匹配属性的对象
我有一个对象列表,需要获取一个属性具有相同值的所有对象,以便进一步处理它们。我在谷歌上搜索过的所有东西都让我知道了我要寻找的价值。相反,我只需要火柴。说我有这个Python获取列表中具有匹配属性的对象,python,list,attributes,generator,Python,List,Attributes,Generator,我有一个对象列表,需要获取一个属性具有相同值的所有对象,以便进一步处理它们。我在谷歌上搜索过的所有东西都让我知道了我要寻找的价值。相反,我只需要火柴。说我有这个 class Person: def __init__(self, name, age): self.name = name self.age = age p1 = Person("mike", 28) p2 = Person("joe", 28) p3 = Person("nick", 27)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("mike", 28)
p2 = Person("joe", 28)
p3 = Person("nick", 27)
p4 = Person("Janet", 27)
people = [p1, p2, p3]
#need something like
matches = getMatches(people, "age")
print matches
[[Mike's object, Joe' Object], [Nick's object, Janet's object]]
我想出了这个,它很管用,但对我来说似乎有点糟糕
def getMatches(objs, attr):
def Gen(objs, attr):
used = [] #already searched for these...
for obj in objs:
a = getattr(obj, attr)
if a not in used:
yield [p for p in objs if getattr(p, attr) == a]
used.append(a)
gen = Gen(objs, attr)
return [g for g in gen]
在我的实际代码中,需要这样做更有意义。有人能帮我清理一下吗?或者有没有一个标准的功能或方法来完成这个我不知道的事情
我很欣赏这些答案,最后使用groupby并确保首先对它们进行排序。这是我第一次尝试编写生成器。如果我可以问一下,我的代码是什么样的,可以说是python式的?您可以这样使用
from operator import attrgetter
from itertools import groupby
def getMatches(people, prop):
people = sorted(people, key = attrgetter(prop))
return [list(grp) for k, grp in groupby(people, attrgetter(prop))]
print getMatches(people, "age")
for group in getMatches(people, "age"):
print [people.name for people in group]
你可以这样检查结果
from operator import attrgetter
from itertools import groupby
def getMatches(people, prop):
people = sorted(people, key = attrgetter(prop))
return [list(grp) for k, grp in groupby(people, attrgetter(prop))]
print getMatches(people, "age")
for group in getMatches(people, "age"):
print [people.name for people in group]
输出
['mike', 'joe']
['nick', 'Janet']
那么:
def get_matches(objs, key):
d = {}
for obj in objs:
d.setdefault(getattr(obj, key), []).append(obj)
return d
之后[我添加了一个repr
]:
>>> get_matches(people, "age")
{27: [{'name': 'nick', 'age': 27}, {'name': 'Janet', 'age': 27}],
28: [{'name': 'mike', 'age': 28}, {'name': 'joe', 'age': 28}]}
它提供的一个优点是,
groupby
通过按连续值分组来进行操作。如果这是你想要的,那就太完美了。否则,您必须记住首先按键函数排序。(这里的缺点是它没有保留键值的出现顺序,尽管我们可以使用collections.OrderedDict
,如果我们真的在意的话。)注意,只有当人员列表按键属性排序时(更具体地说,如果具有相同键的项目一起显示),这才有效。@alexis Yup。你是对的:)我更新了答案。请现在检查。