Creating unique list of objects from multiple lists

我们两清 提交于 2021-02-20 18:50:18

问题


I have defined a custom object with multiple fields.

For example say I have a Student object, which consists of a name, ID, and age. To compare two students and determine whether they are the same student or not, I implemented a __ eq__ method that will return whether the age, name, and ID of the two students match up.

def __eq__(self, other):
   return self.name == other.name and self.ID == other.ID and self.age == other.age

Bear in mind that the student is just an example, so the fact that student ID's tend to be unique is not considered.

Suppose I have the following enrollment lists with an arbitrary number of Student objects

[S1, S2, S3]
[S2, S3]
[S3, S5, S4]
[S1, S4, S2, S1]

I would want to create some data structure that will contain the following elements

S1, S2, S3, S4, S5

The simplest way to do this would be to initialize some data structure that can hold lots of stuff, grab an item, check whether it exists in the structure, and add it if it doesn't.

new_list = some_new_list 
for each list of students:
  for each student in the list:
     check if the student is in new_list
     #decide what to do 

If I decided to implement it as a simple list, I could potentially make a lot of comparisons as my list continues to grow, especially if I have a ridiculous amount of students and enrollment lists.

What is an efficient way of implementing this? Both for comparing two objects and then using that comparison method to generate a unique set of objects.

EDIT: so I tried a simple set implementation.

>>>a = Student("sample", 1234, 18)
>>>b = Student("sample", 1234, 18)
>>>students = set()
>>>students.add(a)
>>>b in students
False
>>>b == a
True

Am I doing something wrong?


回答1:


from itertools import chain
myset = set(chain(iterable1, iterable2, iterable3, iterable4))

You get unique items, and you only iterate over each iterable once. chain makes one long iterable from a series of iterables. If you need it sorted, sorted(myset) will give you a sorted list.

Your Student class needs to implement a __hash__ that is compatible with it's __eq__:

def __hash__(self):
    return (self.name, self.ID, self.age).__hash__()



回答2:


I have but one word for you.

set

Here are the docs for sets



来源:https://stackoverflow.com/questions/7031736/creating-unique-list-of-objects-from-multiple-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!