简体   繁体   中英

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:

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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