Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

return position of a unique value in python array

def partition(array):
  return {i: (array == i).nonzero()[0] for i in np.unique(array)}
Comment

return position of a unique value in python array

In [304]: array = np.array([1, 1, 2, 3, 2, 1, 2, 3])

In [305]: np.unique(array)            # unique values in `array`
Out[305]: array([1, 2, 3])

In [306]: array == 1                  # retrieve a boolean mask where elements are equal to 1
Out[306]: array([ True,  True, False, False, False,  True, False, False])

In [307]: (array == 1).nonzero()[0]   # get the `True` indices for the operation above
Out[307]: array([0, 1, 5])
Comment

python find indexes of unique elements of a list

import numpy as np
np.unique([1,6,6,2,2,3,4,5,5,5], return_index=True)

>>> (array([1, 2, 3, 4, 5, 6]), array([0, 3, 5, 6, 7, 1], dtype=int64))
Comment

PREVIOUS NEXT
Code Example
Python :: add two strings together 
Python :: function with args* example 
Python :: python opencv check image read 
Python :: dictionary append value python 
Python :: Is python statically typed language? 
Python :: how to make a grid in python 
Python :: python merge two list 
Python :: sets in python 
Python :: how to sort dictionary in ascending order by sorted lambda function in python 
Python :: 2d array row and column 
Python :: qpushbutton clicked 
Python :: pos taggging in nltk 
Python :: get length of string python 
Python :: pd df iloc 
Python :: how to invert a true false array in python 
Python :: list to text python 
Python :: slicing strings in python 
Python :: python how to print variable value 
Python :: python print() 
Python :: python function parameters default value 
Python :: class attributes in python 
Python :: perform zero crossing using openCV 
Python :: give cell format to condition pandas dataframe 
Python :: for in list start with index python 
Python :: python open aspx file 
Python :: matplotlib remove white lines between contour 
Python :: python map function 
Python :: 1 12 123 python 
Python :: python string ignore characters 
Python :: get path and name of file for open() 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =