UpmostScarab
UpmostScarab

Reputation: 985

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.

Upvotes: 1

Views: 60

Answers (2)

Eelco Hoogendoorn
Eelco Hoogendoorn

Reputation: 10769

The numpy_indexed package can solve such problems in a vectorized manner:

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

Upvotes: 0

galaxyan
galaxyan

Reputation: 6151

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

output:

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

Upvotes: 2

Related Questions