简体   繁体   English

寻找复杂的独特元素

[英]Finding complicated unique elements

I have two following arrays: 我有以下两个数组:

a = [[1,'string',2,3],[2,'otherstring', 6,1],[1, 'otherstring',2,3]]
b = [[7,'anotherstring',4,3],[1,'string',2,3]]

which in real of course are a lot bigger. 当然,实际上要大得多。 I need to find unique elements: 我需要找到独特的元素:

>>> unique(a,b)
[[1,"string",2,3],[2,'otherstring', 6,1],
    [1, 'otherstring',2,3],[7,'anotherstring',4,3]]

I thought about numpy.unique yet it seems to serve a bit another function, since: 我想到了numpy.unique,但它似乎还提供了另一个功能,因为:

>>> a = np.array([[1, 1], [2, 3]])
>>> np.unique(a)
array([1, 2, 3])

NOTE: list(set(a+b)) doesn't work since list is not hashable. 注意: list(set(a + b))不起作用,因为list不可哈希。

set(tuple(item) for item in a+b)

输出:

set([(2, 'otherstring', 6, 1), (1, 'string', 2, 3), (7, 'anotherstring', 4, 3), (1, 'otherstring', 2, 3)])

The numpy_indexed package can solve such problems in a vectorized manner: numpy_indexed软件包可以矢量化方式解决此类问题:

import numpy_indexed as npi
npi.unique(tuple(zip(a+b)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM