PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 18, 2022

[FIXED] How to generate an array in Python which shows the positions of the highest to the lowest integers or floats in another array?

 October 18, 2022     arrays, integer, python, sorting     No comments   

Issue

I'd like to generate an array which contains the positions of the highest integers/floating point numbers to the lowest in another array. For example:

integers = [1,6,8,5] I want the newly generated array to be: newArray = [2,1,3,0]

or

floatingPoints = [1.6,0.5,1.1] would become newArray = [0,2,1]


Solution

You can use the numpy function argsort and then simply reverse the ordering as it gives you ascending rather than descending, by default:

np.argsort(integers)[::-1]

Example:

import numpy as np
integers = np.array([1, 6, 8, 5])
np.argsort(integers)[::-1]

This results in the desired [2, 1, 3, 0].



Answered By - statnet22
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing