Konversi array 2d menjadi python 1d

Dalam posting ini, kita akan mengeksplorasi metode sederhana untuk mengonversi array NumPy menjadi daftar. Misalnya, kita dapat menggunakan fungsi Python

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
3 atau metode array NumPy
# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
4.  

Konversi array 2d menjadi python 1d

Kedua cara tersebut serupa, tetapi ada beberapa perbedaan yang perlu dipertimbangkan untuk memilih yang tepat untuk setiap aplikasi.  

Daftar Fungsi Python()

Fungsi

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
_3 menerima sebagai input array untuk dikonversi, dan itu setara dengan kode python berikut

my_list = []
for el in my_arr:
    my_list.append(el)

Algoritme ini hanya memproses level pertama dari array yang mempertahankan tipe data skalar NumPy, i. e. , itu akan mengembalikan daftar objek NumPy.  

Tipe skalar NumPy adalah cara yang lebih canggih untuk merepresentasikan data yang berguna untuk perhitungan ilmiah di mana presisi, penggunaan RAM, atau kompatibilitas dengan bahasa pemrograman C sangat penting

Mari kita lihat beberapa contoh kode untuk array dimensi satu, dua, atau lebih dan kasus khusus dimensi nol.  

Kasus 1. Array satu dimensi (1D)

Kami pertama-tama mengimpor paket NumPy dan membuat array sampel. Kemudian, kita dapat membuat array dari tipe data yang dibutuhkan menggunakan parameter opsional

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
6. Dalam hal ini, array bilangan bulat 8 bit, i. e. , kita dapat memiliki bilangan bulat dalam rentang
# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
7

Kemudian kami mengubahnya menjadi daftar menggunakan fungsi

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
8.  

Beberapa baris kode terakhir memeriksa apakah tipe data dari objek yang dihasilkan adalah daftar dan tipe data dari elemen daftar tidak berubah.  

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)

Output dari kode tersebut adalah

Sample array: 
 [1 2 3 4]

Data type of the array's elements: int8

List created: 
 [1, 2, 3, 4]

Object type: <class 'list'>
Data type of the lists's elements: int8
_

Keluarannya menunjukkan bahwa kita mengonversi larik 1D menjadi sebuah daftar. Selain itu, tipe data elemen daftar tidak diubah.  

Kasus 2. Array dua dimensi (2D)

Dalam hal ini, kita mengganti definisi array dengan array dua dimensi bertipe data NumPy

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
9, kemudian kita centang tipe objek yang terdapat pada list dan tipe data salah satu elemennya.  

# converting a 2D array to lists using the function list()
# import numpy package
import numpy as np

# create an 2D array of the default data type (int8):
arr = np.array([[1, 2], [3, 4]], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and the class and data type of one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', str(type(new_list[0])))
print('Data type of an element of the arrays belonging to the list: ', new_list[0].dtype)

Output dari kode tersebut adalah

Sample array: 
 [[1 2]
 [3 4]]

Data type of the array's elements: int8

List created: 
 [array([1, 2], dtype=int8), array([3, 4], dtype=int8)]

Object type: <class 'list'>
Data type of the lists's elements: <class 'numpy.ndarray'>
Data type of an element of the arrays belonging to the list: int8

Perhatikan di sini. fungsi

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
_8 membuat daftar array. Itu tidak membuat daftar daftar, mis. e. , iterasi berhenti pada level pertama

Selain itu, setiap elemen dari array ini masih berupa bilangan bulat 8-bit NumPy. Kita akan melihat bahwa perilaku metode

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
4 berbeda

Kasus 3. array nol dimensi (0D)

Sekarang mari kita lihat kasus tertentu dari array NumPy dengan panjang nol, menggantikan definisi array

# converting a 0D array to lists using the function list()
# import numpy package
import numpy as np

# create a sample 0D array of 8 bit integers:
arr = np.array(7, dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and its element
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)

Menjalankan kode menghasilkan kesalahan

Sample array: 
 7

Data type of the array's elements: int8

Traceback (most recent call last):
 (....)
 File "/#####/NumpyArrayConv.py", line xxx, in <module>
 new_list = list(arr)

TypeError: iteration over a 0-d array

Array dimensi nol bukanlah objek yang dapat diubah, dan kami mendapatkan kesalahan eksekusi

Array dimensi nol adalah kejadian yang tidak biasa. Namun, saat menganalisis kasus tepi atau aplikasi ilmu data di mana array NumPy banyak digunakan, ini adalah sesuatu yang perlu dipertimbangkan

Daftar Metode Numpy Array()

Metode NumPy

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
_4 tidak menerima argumen apa pun dan mengubah array secara iteratif menjadi daftar

Ini berarti bahwa dalam kasus array dua dimensi atau lebih, itu akan mengembalikan daftar bersarang

Setiap elemen daftar kemudian dikonversi dari tipe data skalar NumPy 'ke tipe Python bawaan terdekat yang kompatibel' (i. e. , itu akan mengembalikan daftar objek Python)

Tipe data objek Python kurang canggih dan cocok untuk sebagian besar aplikasi. Misalnya, dalam kasus bilangan bulat, satu-satunya tipe data yang tersedia adalah bilangan bulat 64-bit

Mari kita lihat beberapa contoh cara menggunakan metode

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
4

Kasus 1. Array satu dimensi (1D)

Mirip dengan kasus yang disebutkan di atas, pertama-tama kita mengimpor paket NumPy dan membuat larik contoh

Selanjutnya, kita akan membuat array dari tipe data tertentu menggunakan parameter opsional

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
6

Dalam hal ini, array bilangan bulat 8 bit, i. e. , kita dapat memiliki bilangan bulat dalam rentang

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
7

Kemudian kami mengonversinya menjadi daftar menggunakan metode

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
4.  

Selain itu, dalam hal ini, kami akan memeriksa tipe data dari objek yang dihasilkan untuk menunjukkan perilaku metode

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
4 dan perbedaannya dari fungsi
# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
8

# converting a 1D array to lists using the NumPy method tolist()
# import numpy package
import numpy as np

# create a sample 1D array of 8 bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = arr.tolist()

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', str(type(new_list[0])))

Output dari kode tersebut adalah

Sample array: 
 [1 2 3 4]

Data type of the array's elements: int8

List created: 
 [1, 2, 3, 4]

Object type: <class 'list'>
Data type of the lists's elements: <class 'int'>

Kita dapat melihat bahwa metode

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
_4 membuat daftar objek python dari kelas
# converting a 2D array to lists using the function list()
# import numpy package
import numpy as np

# create an 2D array of the default data type (int8):
arr = np.array([[1, 2], [3, 4]], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and the class and data type of one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', str(type(new_list[0])))
print('Data type of an element of the arrays belonging to the list: ', new_list[0].dtype)
0 yang sesuai dengan bilangan bulat 64-bit alih-alih membuat objek NumPy dari tipe data
# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
9.  

Implikasi utamanya adalah kita tidak akan bisa menggunakan metode array NumPy pada elemen daftar yang baru dibuat

Kasus 2. Array dua dimensi (2D)

Juga, dalam hal ini, kami mengganti definisi array dengan array dua dimensi dari tipe data NumPy

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
9, kemudian kami memeriksa jenis objek yang terdapat dalam daftar dan tipe data dari salah satu elemennya.  

# converting a 2D array to lists using the NumPy method tolist()
# import numpy package
import numpy as np

# create a sample 2D array of 8 bit integers:
arr = np.array([[1, 2], [3, 4]], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = arr.tolist()

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and the class and data type of one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', str(type(new_list[0])))
print('Data type of an element of the nested list: ', str(type(new_list[0][0])))

Output dari kode tersebut adalah

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
0

Kita dapat melihat bahwa output menunjukkan bahwa kita membuat daftar daftar, dan elemen daftar adalah bilangan bulat Python 64-bit standar, i. e. , tipe data elemen daftar telah berubah

Kasus 3. Array nol dimensi (0D)

Mari kita periksa lagi kasus tertentu dari array NumPy dengan panjang nol menggunakan metode

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
4

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
_1

Output dari kode tersebut adalah

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
_2

Kami berhasil mengonversi array nol dimensi menjadi daftar

Satu-satunya elemen dari daftar adalah objek Python dari kelas integer. Seperti pada kasus sebelumnya, menggunakan metode ini akan menghasilkan perubahan tipe data

Kesimpulan

Fungsi Python

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
8 dan metode NumPy
# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
4 sangat mirip, tetapi mereka berperilaku berbeda tergantung pada dimensi array

Dalam kasus array nol dimensi, daftar fungsi akan menghasilkan kesalahan karena array 0D tidak dapat diubah. Namun, jika kami menggunakan metode NumPy

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
4 sebagai gantinya, kami memperoleh daftar objek Python dari tipe data terdekat yang sesuai

Dalam kasus array satu dimensi, perilaku fungsi dan metodenya hampir sama; .

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
_8 mempertahankannya,
# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
4 mengonversi dari tipe data NumPy ke Python

Dalam kasus dua dimensi atau lebih, fungsinya berbeda lagi.

# converting a 1D array to lists using the function list()
# import numpy package
import numpy as np
import sys

# create a sample 1D array of 8-bit integers:
arr = np.array([1, 2, 3, 4], dtype=np.int8)

# let's check the generated sample array and its type
print('\nSample array: \n', arr)
print('\nData type of the array's elements: ', arr.dtype)

# conversion using the function list()
new_list = list(arr)

# check the created object type
print('\nList created: \n', new_list)

# check data types for the created object and one of its elements
print('\nObject type: ', str(type(new_list)))
print('Data type of the lists's elements: ', new_list[0].dtype)
8 mengembalikan daftar objek NumPy, mis. e. , jika mereka adalah array bersarang, itu tidak akan mengubahnya menjadi daftar;

Bagaimana mengubah array 2D menjadi array 1D?

Setiap baris dalam larik 2D Anda ditempatkan dari ujung ke ujung dalam larik 1D Anda. saya memberikan di baris mana Anda berada, dan j memberikan kolom (seberapa jauh ke baris itu). jadi jika Anda berada di baris ke-i, Anda perlu menempatkan i menyelesaikan baris dari ujung ke ujung, lalu menambahkan j lagi ke baris itu untuk mendapatkan indeks larik tunggal Anda . Simpan jawaban ini.

Bagaimana Anda mengonversi ke array 1D dengan Python?

Perataan array berarti mengubah array multidimensi menjadi array 1D. Kita dapat menggunakan membentuk kembali(-1) untuk melakukan ini.

Bagaimana cara membentuk ulang array NumPy menjadi 1D?

Anda dapat menggunakan np. reshape(arr,-1) , Gunakan -1 sebagai bentuk , sehingga akan mengubah larik bentuk apa pun menjadi larik datar. Ikuti contoh di bawah ini untuk mengonversi larik multidimensi menjadi larik 1-D. Sebagai alternatif, Anda juga dapat menggunakan fungsi reshape(-1) untuk mengubah array multidimensi menjadi array 1D.

Bagaimana Anda meratakan array dengan Python?

Dengan menggunakan ndarray. flatten() function kita dapat meratakan matriks menjadi satu dimensi dengan python. memesan. 'C' berarti meratakan baris-utama. 'F' berarti meratakan kolom-utama.