Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

395
Vistas
numpy.unique sort based on counts

The numpy.unique function allows to return the counts of unique elements if return_counts is True. Now the returned tuple consists of two arrays one containing the unique elements and the 2nd one containing a count array, both are sorted by the unique elements. Now is there a way to have both sorted according to the counts array instead of the unique elements? I mean I know how to do it the hard way but is there some concise one-liner or lambda functionality for such cases?

Current result:

my_chr_list = ["a","a","a", "b", "c", "b","d", "d"]
unique_els, counts = np.unique(my_chr_list, return_counts=True)
print(unique_els, counts)

Which returns something along the lines of this:

>>> (array(['a', 'b', 'c', 'd'], 
     dtype='<U1'), array([3, 2, 1, 2], dtype=int64))

However, what I would want to have:

>>> (array(['a', 'b', 'd', 'c'], 
     dtype='<U1'), array([3, 2, 2, 1], dtype=int64))
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can't do this directly with unique function. Instead as a Numpythonic approach, you can use return_index keyword to get the indices of the unique items then use np.argsort to get the indices of the sorted count items and use the result to find the items based on their frequency.

In [33]: arr = np.array(my_chr_list)

In [34]: u, count = np.unique(my_chr_list, return_counts=True)

In [35]: count_sort_ind = np.argsort(-count)

In [36]: u[count_sort_ind]
Out[36]: 
array(['a', 'b', 'd', 'c'], 
      dtype='<U1')

In [37]: count[count_sort_ind]
Out[37]: array([3, 2, 2, 1])
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda