I have a numpy array A which looks like this:
array([list(['nan', 'nan']), list(['nan', 'nan', 'apple', 'apple', 'banana', 'nan', 'nan']), list(['red', 'red']), ..., list(['nan', 'festival'])], dtype=object)
I want to convert this to an array in which each list contains only unique elements. For example, I want the above array to get converted to:
['nan'],['nan','apple','banana'],['red'],...,['nan','festival']
I have tried doing this:
output = [] for i in A: output.append(np.unique(i)) output
The output which I get doing this is not desired and currently looks like this:
[array(['nan'], dtype='<U3'), array(['nan'], dtype='<U3'), array(['nan'], dtype='<U3'),....]
What can be done?
Advertisement
Answer
arr=np.array([list(['nan', 'nan']), list(['nan', 'nan', 'apple', 'apple', 'banana', 'nan', 'nan']), list(['red', 'red']), ..., list(['nan', 'festival'])], dtype=object)
try via list comprehension:
out=[np.unique(x).tolist() for x in arr]
OR
out=[list(np.unique(x)) for x in arr]
output of out
:
[['nan'], ['apple', 'banana', 'nan'], ['red'], [Ellipsis], ['festival', 'nan']]