[英]Group objects with an identical list attribute
I have two classes:我有两个班级:
class A:
def __init__(self, name, li):
self.b_list = li
class B:
def __init__(self, i):
self.i = i
class A
contains a list of objects of type class B
. class A
包含class B
类型的对象列表。
Assuming I have a list of class A
objects, how can I group the class A
objects that have an identical b_list
together?假设我有一个class A
对象的列表,如何将具有相同b_list
class A
对象b_list
在一起?
For example:例如:
a_list = []
li = [B(1), B(2), B(3)]
a_list.append(A(li))
li = [B(2), B(3)]
a_list.append(A(li))
li = [B(1), B(2), B(3)]
a_list.append(A(li))
After processing this should give us two lists, one list with the first and third A
, and another list with only the second A
.处理后这应该给我们两个列表,一个列表包含第一个和第三个A
,另一个列表只有第二个A
。 Or in a nutshell:或者简而言之:
result = [
[ A([B(1),B(2),B(3)]), A([B(1),B(2),B(3)]) ],
[ A([B(2),B(3)] ]
]
For starters, I've removed the name
parameter from class A, since the rest of your details omitted it.首先,我已经从 A 类中删除了name
参数,因为您的其余详细信息都省略了它。
To group your class A objects together, you're going to need to define exactly what is meant when two A objects are equal.要将 A 类对象组合在一起,您需要准确定义两个 A 对象相等时的含义。 I've created a __cmp__
method that will let us sort on A objects by comparing them.我创建了一个__cmp__
方法,让我们通过比较它们来对 A 对象进行排序。
Now, since your A objects are composed of B objects, you're going to need something to define what is meant by two B objects being equal.现在,由于您的 A 对象由 B 对象组成,您将需要一些东西来定义两个 B 对象相等的含义。 I've created a __eq__
method in class B that does that.我在 B 类中创建了一个__eq__
方法来做到这一点。
Next, I've sorted the A instances to make grouping them easier, and added a __str__
method to class A, and a __repr__
method to class B so you can verify that they are being grouped together correctly.接下来,我已经整理了一个实例,以使它们组合更容易,并且加入了__str__
方法A类和__repr__
方法,B类,以便您可以验证它们被组合在一起正确。
I haven't added any error checking anywhere, so this code is a little fragile.我没有在任何地方添加任何错误检查,所以这段代码有点脆弱。
class A:
def __init__(self, li):
self.b_list = li
def __cmp__(self, other):
return cmp([elem.i for elem in self.b_list],
[elem.i for elem in other.b_list])
def __str__(self):
return "A({})".format(self.b_list)
class B:
def __init__(self, i):
self.i = i
def __eq__(self, other):
return self.i == other.i
def __repr__(self):
return "B({})".format(self.i)
def main():
a_list = []
li = [B(1), B(2), B(3)]
a_list.append(A(li))
li = [B(2), B(3)]
a_list.append(A(li))
li = [B(1), B(2), B(3)]
a_list.append(A(li))
result = []
last_item = None
for item in sorted(a_list):
if last_item and item == last_item:
result[-1] = result[-1] + [item]
else:
result.append([item])
last_item = item
for row in result:
print [str(elem) for elem in row]
if __name__ == '__main__':
main()
Result:结果:
['A([B(1), B(2), B(3)])', 'A([B(1), B(2), B(3)])']
['A([B(2), B(3)])']
You can sort a_list
using the attribute b_list as a key.您可以使用属性 b_list 作为键对a_list
进行排序。
a_list.sort(key=lambda a:a.b_list)
or:或者:
from operator import itemgetter
a_list.sort(key=itemgetter("b_list"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.