0

I would like to get the unique element from an array with specific sequences.

For example,

initially, I have array as following:

array([3, 3, 6, 6, 5, 5, 5, 5, 2, 8, 8])

I would like to get the unique element from the array above and maintain its original order sequence. The sample output as following:

array([3, 6, 5, 2, 8])

I know that using numpy.unique() will get the unique elements for the array, but it will return sorted unique elements. However, in my case, I need the output in the original sequence.

To generate toy example, here easy for you to just copy & paste

arr = np.array([3,3,6,6,5,5,5,5,2,8,8])

At the time I post this, I still working on this, and I wish to get your suggestion so that I could move to the next part of work. Your advice will be highly appreciated. Thank you for your time!

Yeo Keat
  • 143
  • 1
  • 9
  • 4
    Just loop over the array elements, collecting the unique elements into a list, then convert the list back into an array. – Barmar Jan 26 '21 at 06:17
  • 1
    What would be the result for `[3, 6, 6, 3]` or can't this happen? – Michael Butscher Jan 26 '21 at 06:18
  • 1
    I assume it would be [3, 6] – John Krakov Jan 26 '21 at 06:21
  • @Barmar I'm having a hard time imagining how to *collecting the unique elements into a list*. A dict/set, yes. A list alone, not quite clear how. That said, this sounds like just `np.unique(arr)`. – Quang Hoang Jan 26 '21 at 06:26
  • Loop through the array elements. If the array element is already in the result list, skip it, otherwise append it to the result. – Barmar Jan 26 '21 at 06:27
  • 1
    You could also use a list comprehension, although a for loop would be faster : `[x for i, x in enumerate(arr) if arr.index(x) == i]` – John Krakov Jan 26 '21 at 06:28
  • 1
    @Barmar that's a `O(n^2)` approach when there are no duplicates, while `dict/set` approach is most likely `O(n)` or `O(n logn)`. Still surprise no suggestion for `np.unique`. – Quang Hoang Jan 26 '21 at 06:33
  • 1
    something like this @QuangHoang? import numpy as np arr = np.array([3,3,6,6,5,5,5,5,2,8,8]) _, idx = np.unique(arr, return_index=True) print(arr[np.sort(idx)]) – mpx Jan 26 '21 at 07:09
  • 1
    @balandongiv yes indeed! Forgot that unique sorts the values. – Quang Hoang Jan 26 '21 at 07:13
  • 1
    IIUIC, OP request `preserved order` @QuangHoang, hence the above suggestion – mpx Jan 26 '21 at 07:13
  • Thank you everyone! I truly appreciate your valuable advice and comment. It is really awesome and I enjoy to discuss here. For this question, I received private feedback stated that this question has been asked in another post, but I found that the post given is actually refer to list, not array. Thus, I will keep this post. Thank you again! – Yeo Keat Jan 26 '21 at 07:23
  • @QuangHoang I was just trying to point him in the right direction, not provide the best solution. I'm trying not to write the code for him. – Barmar Jan 26 '21 at 07:55
  • Thank you @Barmar I appreciate your guidance! =) – Yeo Keat Jan 26 '21 at 08:08

1 Answers1

2

Try this snippet:

arr = np.array([3,3,6,6,5,5,5,5,2,8,8])
unique = []
[unique.append(n) for n in arr if n not in unique]
print(np.array(unique))
Keziya
  • 96
  • 5
  • Hi Keziya, thank you for your answer! I have tried the snippet, but I get the result as this `[None, None, None, None, None]`. Then I rearrange the line to make it a nested loop and got the correct result. – Yeo Keat Jan 26 '21 at 08:00