Home > OS >  How can i reorder a numpy array from high to low in another array without using in built functions
How can i reorder a numpy array from high to low in another array without using in built functions

Time:12-18

i am having a bit of trouble here, plus i am new to using numpy. what i want to do is reorder the array T in V from high to low and changing the highest number to -1 everytime in the array T without using unbuilt functions i would truly appreciate any help, thank you.

from copy import *
from numpy import *
import numpy



n = int(input("Entrez un entier: "))

def control_n(n):
    while n < 3 or n > 20:
        n = int(input("Rentrez un entier: "))
    return n


control_n(n)

# Initialisation du tableau, de taille n , type entier
t = array(n * [0], dtype=int)
v = array(n * [0], dtype=int)


            
def maximum(t, n):
    max = 0
    for i in range(n):
        if t[i] > max:
            max = t[i]
            print(max)
    return max, i


def rearrange(t, n, v):
    for i in range(n):
        max, indice = maximum(t, n)
        v[i] = max
        t[indice] = -1

    return (t, v)


    t1, v1 = rearrange(t, n, v)
    print(t1)
    print(v1

)

CodePudding user response:

For n == 5 I created both arrays as:

np.random.seed(1)
t = np.random.randint(1, 20, n)
v = np.random.randint(1, 20, n)

I used np.random.seed to get repeatable result.

The content of both arrays is:

array([ 6, 12, 13,  9, 10])
array([12,  6, 16,  1, 17]))

Then, to sort v array in the order of elements in t, you can run:

t1 = np.argsort(t)
v1 = v[t1]

The result is:

array([0, 3, 4, 1, 2], dtype=int64)
array([12,  1, 17,  6, 16])

Or shorter, without explicit creation of t1, you can run:

v1 = v[np.argsort(t)]

Edit

To get the expected result without Numpy functions, you can proceed as follows:

  1. Define maximum function the following way:

    def maximum(t, n):
        max = 0
        iMax = -1
        for i in range(n):
            if t[i] > max:
                max = t[i]
                iMax = i
        return max, iMax
    

    A change compared to your solution is that this function returns the index of the max element in t, whereas your code returned the index of the last element (every time the same).

  2. Define rearrange function as:

    def rearrange(t, n, v):
        t1 = np.copy(t)
        tInd = np.zeros_like(t)  # Table of indices
        for i in range(n):
            max, ind = maximum(t1, n)
            tInd[i] = ind
            t1[ind] = -1
        tInd = tInd[::-1]
        return tInd, v[tInd]
    

    The differences are that my code:

    • does not "destroy" source arrays (a good practice),
    • first computes the index array (tInd),
    • but it is generated as the list of indices starting from the max value, so to get the expected (ascending) order, it is reversed,
    • v1 is generated in one go, as late as in return statement, retrieving elements of v in the order defined by tInd.

Now when you run rearrange(t, n, v), you will get the expected result.

CodePudding user response:

Ignore the comments they are for my teacher, i had to make another loop because somehow it wasn't giving me the correct index number.

this is the full code.

from copy import *
from numpy import *
import numpy


n = int(input("Entrez un entier: "))

# controle de saisie sur n
def control_n(n):
    while n < 3 or n > 20:
        n = int(input("Rentrez un entier: "))
    return n


control_n(n)

# Initialisation du tableau, de taille n , type entier
t = array(n * [0], dtype=int)
v = array(n * [0], dtype=int)


# Procedure to remplissage du tableaux
def remplir_t(n, t):
    # un boucle pour la remplissage
    for i in range(n):
        # le entier qui doit etre postive
        b = int(input("Entrez un entier positive: "))
        while b < 0:
            b = int(input("Rentrez un entier positive: "))
        # si est il positive on le ajouter dans le tableaux sinon le utilisateur doit entrez un entier positive
        t[i] = b
    return t


remplir_t(n, t)

# Une fonction pour le decouvrir le maximum dans le array numpy
def maximum(t, n):
    max = 0
    # J'ai essayé d'utiliser le premier "i" de la boucle mais c'est toujours incorrect
    # alors j'ai fait une autre boucle pour trouver l'indice
    for i in range(n):
        if t[i] > max:
            max = t[i]
    for i in range(n):
        if max == t[i]:
            index = i
    return max, index


def rearrange(t, v, n):
    # J'ai copié le tableau comme vous l'avez dit merci pour l'aide.
    tableaux_intial = t.copy()
    for i in range(n):
        max, indice = maximum(t, n)
        # utilisez la fonction pour trouver le nombre maximum
        # puis ajoutez-le au tableau V et changez sa valeur en -1 dans le tableau T
        v[i] = max
        t[indice] = -1
    return t, v, tableaux_intial


t, v, tableaux_intial = rearrange(t, v, n)
# le affichage
print("Le tableaux avec les -1: ", t)
print("Le tableaux initial : ", tableaux_intial)
print("Le tableaux V: ", v)
  • Related