Kā mēs zinām Masīvs ir vienumu kolekcija, kas glabājas blakus esošās atmiņas vietās. Programmā Python a Saraksts ( Dinamiskais masīvs ) var uzskatīt par masīvu. Šajā rakstā mēs uzzināsim, kā inicializēt noteikta izmēra tukšu masīvu. Apskatīsim dažādus Pythonic veidus, kā izveidot tukšu sarakstu Python ar noteiktu izmēru.
Python — inicializē noteikta garuma tukšu masīvu
Tālāk ir norādītas metodes, kā inicializēt noteikta garuma tukšu masīvu Python.
- Izmantojot * Operatoru
- Saraksta izpratnes izmantošana
- For Loop izmantošana
- Izmantojot NumPy
- Izmantojot atkārtošanas () metodi
1. metode: inicializējiet tukšu masīvu, izmantojot * operatoru
Šajā piemērā mēs izveidojam dažāda veida tukšumus, izmantojot an zvaigznītes (*) operators.
Python3
# initializes all the 10 spaces with 0’s a = [0] * 10 print('Intitialising empty list with zeros: ', a) # initializes all the 10 spaces with None b = [None] * 10 print('Intitialising empty list of None: ', b) # initializes a 4 by 3 array matrix all with 0's c = [[0] * 4] * 3 print('Intitialising 2D empty list of zeros: ', c) # empty list which is not null, it's just empty. d = [] print('Intitialising empty list of zeros: ', d)> Izvade
Creating empty list contains zeros: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Creating empty list of None: [None, None, None, None, None, None, None, None, None, None] Creating 2D empty list of zeros: [[0, 0,...>
2. metode: izveidojiet tukšu sarakstu python ar noteiktu izmēru, izmantojot saraksta izpratni
Šajā piemērā mēs izmantojam Python Saraksta izpratne 1D un 2D tukšiem masīviem.
Python3 # initialize the spaces with 0’s with # the help of list comprehensions a = [0 for x in range(10)] print(a) b = [[0] * 4 for i in range(3)] print(b)>
Izvade
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]>
3. metode: izveidojiet tukšu noteikta izmēra sarakstu Python, izmantojot cilpu
Šajā piemērā mēs izmantojam a Python cilpa 1D un 2D tukšiem masīviem.
java burbuļu šķirošanaPython3
b= [] for x in range(5): b.append([[]]) print(b) c = [] for x in range(5): c.append(0) print(c)>
Izvade
[[[]], [[]], [[]], [[]], [[]]] [0, 0, 0, 0, 0]>
4. metode: inicializējiet tukšu masīvu, izmantojot Neskaidrs
Šajā metodē mēs izmantojam Numpy modulis lai ģenerētu tukšu 1D un 2D izmēru matricu, izmantojot np.empty() .
Python3 import numpy # create a simple array with numpy empty() a = numpy.empty(5, dtype=object) print(a) # create multi-dim array by providing shape matrix = numpy.empty(shape=(2, 5), dtype='object') print(matrix)>
Izvade
[None None None None None] [[None None None None None] [None None None None None]]>
5. metode: inicializējiet tukšu masīvu, izmantojot atkārtošanas() metodi
Šajā metodē mēs varam izmantot itertools moduļa funkciju repeat(), lai izveidotu jaunu iteratoru, kas atgriež vienu un to pašu vērtību noteiktu skaitu reižu. To var izmantot, lai izveidotu tukšu noteikta izmēra masīvu, norādot vērtību Nav un vēlamo garumu.
Python3 import itertools #Initialize empty array with length 10 filled with 0's a = list(itertools.repeat(0, 10)) print(a) #Initialize 2D empty array with 3 rows and 4 columns filled with 0's b = [list(itertools.repeat(0, 4)) for i in range(3)] print(b)>
Izvade
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]>
Šīs metodes laika sarežģītība ir O(n) un palīgtelpa O(n), kur n ir vēlamais masīva garums.
Atsauce: Saraksts Python