np.unique needs an option to retain the original order
See original GitHub issueThis would integrate the work-around solutions of a number of stackoverflow
questions, such as:
http://stackoverflow.com/questions/15637336/numpy-unique-with-order-preserved
['b','b','b','a','a','c','c']
numpy.unique gives
['a','b','c']
How can I get the original order preserved
['b','a','c']
The issue with these workarounds is that you can get the unique list unsorted but it is much more complicated to get a matching list with counts (from the return_counts
boolian).
Having an integrated boolian (i.e. is_sorted=True
) for sorting of results would clear up this issue and increase the utility of the function.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:21
- Comments:10 (3 by maintainers)
Top Results From Across the Web
numpy.unique with order preserved - python - Stack Overflow
Use the return_index functionality of np.unique . That returns the indices at which the elements first occurred in the input.
Read more >numpy.unique — NumPy v1.24 Manual
Returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements: the indices of the...
Read more >NumPy: unique() function - w3resource
numpy.unique() function. The unique() function is used to find the unique elements of an array.Returns the sorted unique elements of an ...
Read more >Python List: Remove Duplicates and Keep the Order - Finxter
The index values of the unique items can be stored by using the np.unique() function with the return_index parameter set to True ....
Read more >Numpy Unique, Explained - Sharp Sight
When return_index = True , np.unique will return the index of the first occurrence of the unique value. This parameter is optional.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I would need this functionality as well…
Regarding the ambiguity discussion: FWIW, pandas has implemented a “stable” (does not change order) unique, with the unambiguous choice of using either the
'first'
or the'last'
occurrence of an element for determining the position in the unique output.+1