logo

Konwertuj listę Pythona na tablice NumPy

Wstęp

W Pythonie lista jest liniową strukturą danych, w której mogą być przechowywane heterogeniczne elementy. Nie trzeba go definiować i można go zmniejszać i rozszerzać w razie potrzeby. Z drugiej strony tablica NumPy to struktura danych, w której można przechowywać jednorodne elementy. Jest zaimplementowany w Pythonie przy użyciu biblioteki NumPy. Ta biblioteka jest bardzo wydajna w obsłudze tablic wielowymiarowych. Jest także bardzo wydajny w obsłudze ogromnej liczby elementów danych. Tablice NumPy zużywają mniej pamięci niż struktury danych List. Zarówno tablicę NumPy, jak i listę można rozpoznać po wartości indeksu.

Biblioteka NumPy udostępnia dwie metody konwertowania list na tablice w Pythonie.

  1. Korzystanie z numpy.array()
  2. Używanie numpy.asarray()

Metoda 1: Używanie numpy.array()

W Pythonie najprostszym sposobem przekonwertowania listy na tablicę NumPy jest użycie funkcji numpy.array(). Pobiera argument i zwraca tablicę NumPy. Tworzy nową kopię w pamięci.

Program 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) 

Wyjście:

 List: [1, 2, 3, 4, 5, 6, 7, 8, 9] Array: [1 2 3 4 5 6 7 8 9] 
Konwertuj listę Pythona na tablice NumPy

Metoda 2: Używanie numpy.asarray()

W Pythonie drugą metodą jest funkcja numpy.asarray(), która konwertuje listę na tablicę NumPy. Pobiera argument i konwertuje go na tablicę NumPy. Nie tworzy nowej kopii w pamięci. W tym przypadku wszystkie zmiany wprowadzone w oryginalnej tablicy są odzwierciedlane w tablicy NumPy.

szybkie sortowanie

Program 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) 

Wyjście:

 List: [1, 2, 3, 4, 5, 6, 7, 8, 9] Array: [1 2 3 4 5 6 7 8 9] 
Konwertuj listę Pythona na tablice NumPy

Program 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) 

Wyjście:

 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] 
Konwertuj listę Pythona na tablice NumPy