logo

Converti elenco Python in array NumPy

introduzione

In Python, una lista è una struttura dati lineare che può contenere elementi eterogenei. Non ha bisogno di essere definito e può restringersi ed espandersi secondo necessità. D'altra parte, un array NumPy è una struttura dati che può memorizzare elementi omogenei. È implementato in Python utilizzando la libreria NumPy. Questa libreria è molto efficiente nella gestione di array multidimensionali. È anche molto efficiente nella gestione di un numero enorme di elementi di dati. Gli array NumPy utilizzano meno memoria delle strutture dati List. Sia l'array NumPy che l'elenco possono essere identificati dal relativo valore di indice.

La libreria NumPy fornisce due metodi per convertire elenchi in array in Python.

  1. Utilizzando numpy.array()
  2. Utilizzando numpy.asarray()

Metodo 1: utilizzo di numpy.array()

In Python, il modo più semplice per convertire un elenco in un array NumPy è con la funzione numpy.array(). Richiede un argomento e restituisce un array NumPy. Crea una nuova copia in memoria.

Programma 1

 # importing library of the array in python import numpy # initilizing elements of the list a = [1, 2, 3, 4, 5, 6, 7, 8, 9] # converting elements of the list into array elements arr = numpy.array(a) # displaying elements of the list print ('List: ', a) # displaying elements of the array print ('Array: ', arr) 

Produzione:

 List: [1, 2, 3, 4, 5, 6, 7, 8, 9] Array: [1 2 3 4 5 6 7 8 9] 
Converti elenco Python in array NumPy

Metodo 2: utilizzo di numpy.asarray()

In Python, il secondo metodo è la funzione numpy.asarray() che converte un elenco in un array NumPy. Prende un argomento e lo converte nell'array NumPy. Non crea una nuova copia in memoria. In questo, tutte le modifiche apportate all'array originale si riflettono sull'array NumPy.

Programma 2

 # importing library of the array in python import numpy # initilizing elements of the list a = [1, 2, 3, 4, 5, 6, 7, 8, 9] # converting elements of the list into array elements arr = numpy.asarray(a) # displaying elements of the list print ('List:', a) # displaying elements of the array print ('Array: ', arr) 

Produzione:

 List: [1, 2, 3, 4, 5, 6, 7, 8, 9] Array: [1 2 3 4 5 6 7 8 9] 
Converti elenco Python in array NumPy

Programma 3

 # importing library of the NumPy array in python import numpy # initilizing elements of the list lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] # converting elements of the list into array elements arr = numpy.asarray(lst) # displaying elements of the list print ('List:', lst) # displaying elements of the array print ('arr: ', arr) # made another array out of arr using asarray function arr1 = numpy.asarray(arr) #displaying elements of the arr1 before the changes made print('arr1: ' , arr1) #change made in arr1 arr1[3] = 23 #displaying arr1 , arr , list after the change has been made print('lst: ' , lst) print('arr: ' , arr) print('arr1: ' , arr1) 

Produzione:

 List: [1, 2, 3, 4, 5, 6, 7, 8, 9] arr: [1 2 3 4 5 6 7 8 9] arr1: [1 2 3 4 5 6 7 8 9] lst: [1, 2, 3, 4, 5, 6, 7, 8, 9] arr: [ 1 2 3 23 5 6 7 8 9] arr1: [ 1 2 3 23 5 6 7 8 9] 
Converti elenco Python in array NumPy