2016-07-15 58 views
1

我有以下兩個數組:尋找複雜的獨特元素

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

這當然實際是大了很多。 我需要找到獨特的元素:

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

我想過numpy.unique但它似乎成爲了一下另一個函數,因爲:

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

注:列表(組(A + B))不起作用,因爲列表不可排除。

+1

有與轉動你的子列表到臺/ frozensets一個問題嗎? –

+0

@WayneWerner我認爲這隻有在將它們轉換成似乎是超複雜方式的元組時纔有效。 – UpmostScarab

回答

2
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)]) 
+0

這是一種方法。然而我會使用map:** set(map(tuple,a + b))** – UpmostScarab

+1

不需要表達式中的[] []。 – Elazar

+0

@Elazar是的。謝謝 – galaxyan

0

numpy_indexed包可以解決一個量化的方式這樣的問題:

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