从多个列表创建唯一的对象列表 [英] Creating unique list of objects from multiple lists

查看:33
本文介绍了从多个列表创建唯一的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个包含多个字段的自定义对象.

例如,假设我有一个 Student 对象,它由姓名、ID 和年龄组成.为了比较两个学生并确定他们是否是同一个学生,我实现了一个 __ eq__ 方法,该方法将返回两个学生的年龄、姓名和 ID 是否匹配.

<前>def __eq__(自我,其他):返回 self.name == other.name 和 self.ID == other.ID 和 self.age == other.age

请记住,学生只是一个例子,因此不考虑学生 ID 往往是唯一的这一事实.

假设我有以下注册列表,其中包含任意数量的 Student 对象

<前>[S1、S2、S3][S2、S3][S3、S5、S4][S1、S4、S2、S1]

我想创建一些包含以下元素的数据结构

<前>S1、S2、S3、S4、S5

最简单的方法是初始化一些可以容纳很多东西的数据结构,抓取一个项目,检查它是否存在于结构中,如果不存在则添加它.

<前>new_list = some_new_list对于每个学生名单:对于列表中的每个学生:检查学生是否在 new_list 中#决定做什么

如果我决定将它作为一个简单的列表来实现,那么随着我的列表不断增长,我可能会进行很多比较,尤其是当我有大量学生和注册列表时.

什么是实现此目的的有效方法?两者都用于比较两个对象,然后使用该比较方法生成一组唯一的对象.

所以我尝试了一个简单的集合实现.

<前>>>>a = Student("样本", 1234, 18)>>>b = Student("样本", 1234, 18)>>>学生 = set()>>>students.add(a)>>>b 在学生中错误的>>> b == a真的

我做错了什么吗?

解决方案

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

您会获得独特的项目,并且您只对每个可迭代对象迭代一次.chain 从一系列迭代中生成一个长迭代.如果你需要排序,sorted(myset) 会给你一个排序列表.

您的Student 类需要实现一个与其__eq__ 兼容的__hash__:

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

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?

解决方案

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__()

这篇关于从多个列表创建唯一的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆