0

Someone knows how to delete duplicate elements from a numpy array whose data type is object ?

For instance,

example=array([[
    array([['first', array([[1, 2, 3]]), 1]], dtype=object),
    array([['second', array([[4, 5, 6]]), 2]], dtype=object),
    array([['first', array([[1, 2, 3]]), 1]], dtype=object)]],
  dtype=object)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Mingming Qiu
  • 333
  • 4
  • 9
  • @Pranav This isn't a feature implementation request, and it's not a debugging question so a [mre] is not applicable. That said, it's not clear if OP has done any research, so some criticism is warranted. – wjandrea Aug 16 '21 at 17:11
  • @wjandrea Any question that says "How do I do X" without showing OP's attempt to do it (or OP's research and a clear explanation of why said research didn't help) is a feature implementation request in my book. Once OP has made an attempt, they should include a MRE if they run into problems. – Pranav Hosangadi Aug 16 '21 at 17:13
  • @Pranav Fair enough, though calling it a feature is confusing. When I think "feature", I think multiple connected components that make something useful, whereas this should be only one component -- one line of numpy code. – wjandrea Aug 16 '21 at 17:16
  • 1
    @wjandrea I guess I should change my canned text to _"Implement this for me"_ :) – Pranav Hosangadi Aug 16 '21 at 17:17
  • This may be a duplicate of [Find unique rows in numpy.array](/q/16970982/4518341), though I just tried the top answer and it doesn't work for `dtype=object`. – wjandrea Aug 16 '21 at 17:19
  • I suspect you tried `np.unique` and got a "not implemented for object dtype" error. If so you really should have made this clear. In any case there isn't a simple `numpy` fix for this. Those entries can't be sorted, so `unique` can't work. Object dtype arrays are like lists, containing references, not values. So some sort of iteration (and maybe dict) will be required. @wjandrea – hpaulj Aug 16 '21 at 17:19
  • Sorry for the unclear question, yes, I've tried several methods including numpy.unique, and when I used numpy.unique, it showed the error "TypeError: The axis argument to unique is not supported for dtype object". Nevertheless, it seems that there is no other built-in methods which can spend less time except using the iteration. Thanks for the comments to standardize the question format. – Mingming Qiu Aug 16 '21 at 19:41

0 Answers0