从numpy的阵列的列表中删除重复项 [英] Removing duplicates from a list of numPy arrays

查看:5019
本文介绍了从numpy的阵列的列表中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含(多维)numpy的阵列,所有相同的形状,并且具有相同数目的值的普通Python列表。一些列表中的阵列是较早的重复。

I have an ordinary Python list that contains (multidimensional) numPy arrays, all of the same shape and with the same number of values. Some of the arrays in the list are duplicates of earlier ones.

我有我想删除所有重复的问题,但事实上,数据类型为numpy的数组复杂这个有点...

I have the problem that I want to remove all the duplicates, but the fact that the data type is numPy arrays complicates this a bit...

•我无法使用set()作为numpy的数组不是哈希。结果
•我不能插入期间检查重复,因为阵列在由函数分批生成并添加到列表中以.extend()。结果,
•numpy的数组不是不诉诸的numpy的自身功能之一直接的可比性,所以我不能只是去的东西,用如果x在列表......结果
•列表的内容需要在过程结束时仍然numpy的阵列;我可以比较转换为嵌套列表的数组副本,但我不能数组转换为永久直Python列表。

• I can't use set() as numPy arrays are not hashable.
• I can't check for duplicates during insertion, as the arrays are generated in batches by a function and added to the list with .extend().
• numPy arrays aren't directly comparable without resorting to one of numPy's own functions, so I can't just go something that uses "if x in list"...
• The contents of the list need to remain numPy arrays at the end of the process; I could compare copies of the arrays converted to nested lists, but I can't convert the arrays to straight python lists permanently.

我如何能有效地去除这里重复的有什么建议?

Any suggestions on how I can remove duplicates efficiently here?

推荐答案

在这里使用的解决方案:<一href=\"http://stackoverflow.com/questions/16589791/most-efficient-property-to-hash-for-numpy-array\">Most高效的属性哈希numpy的阵列我们看到,散列效果最好a.tostring()如果是一个numpy的数组。所以:

Using the solutions here: Most efficient property to hash for numpy array we see that hashing works best with a.tostring() if a is an numpy array. So:

import numpy as np
arraylist = [np.array([1,2,3,4]), np.array([1,2,3,4]), np.array([1,3,2,4])]
L = {array.tostring(): array for array in arraylist}
L.values() # [array([1, 3, 2, 4]), array([1, 2, 3, 4])]

这篇关于从numpy的阵列的列表中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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