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

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

问题描述

我有一个普通的Python列表,它包含(多维)numPy数组,所有的数组都是相同的形状,数值相同。列表中的某些数组是较早版本的数组。



我有问题要删除所有重复的内容,但事实上数据类型是numPy数组使这一点复杂化...



•我不能使用set()作为numPy数组不可哈希。

•我可以'在插入期间检查重复项,因为数组是通过一个函数分批生成的,并添加到.extend()的列表中。

•numPy数组不直接比较,而不使用numPy自己的数组功能,所以我不能只是去使用如果x在列表中...

•列表的内容需要在进程结束时保留numPy数组;我可以比较转换为嵌套列表的数组的副本,但是我不能永久地将数组转换成直接的python列表。



有关如何有效删除重复项的任何建议这里?

解决方案

使用以下解决方案:

  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 array中的数组}
L.values()#[array([1,3,2,4]),array([1,2,3,4])]
/ pre>

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.

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...

• 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?

解决方案

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天全站免登陆