Pole v Pythone | Sada 2 (dôležité funkcie)
Pole v Pythone | Súprava 2
Nižšie sú uvedené niektoré ďalšie užitočné funkcie poskytované v Pythone pre polia:
Funkcia typového kódu poľa
Táto funkcia vracia dátový typ, ktorým je pole inicializované. V tomto príklade používame arr.typecode na zistenie dátového typu inicializácie poľa.
Python3 # importing 'array' for array operations import array # initializing array with array values arr = array . array ( 'i' [ 1 2 3 1 2 5 ]) # using typecode to print datatype of array print ( 'The datatype of array is : ' ) print ( arr . typecode )
Výstup
The datatype of array is : i
Veľkosť položiek poľa Funkcia
Táto funkcia vracia veľkosť v bajtoch a s prvok jedného poľa. V tomto príklade používame funkciu itemsize na zistenie veľkosti prvku poľa v byte.
Python3 # importing 'array' for array operations import array # initializing array with array values arr = array . array ( 'i' [ 1 2 3 1 2 5 ]) # using itemsize to print itemsize of array print ( 'The itemsize of array is : ' ) print ( arr . itemsize )
Výstup
The itemsize of array is : 4
buffer_info() v Pythone
Vráti n-ticu predstavujúcu adresa, v ktorej je pole uložené a počet prvkov v ňom. V tomto príklade na to isté používame buffer_info().
Python3 # importing 'array' for array operations import array # initializing array with array values arr = array . array ( 'i' [ 1 2 3 1 2 5 ]) # using buffer_info() to print buffer info. of array print ( 'The buffer info. of array is : ' ) print ( arr . buffer_info ())
Výstup
The buffer info. of array is : (140491260368688 6)
count() v Pythone
Python count() funkcia počíta počet výskytov argumentu uvedeného v poli.
extend() v Pythone
Táto funkcia pripojí celé pole uvedené v jeho argumentoch k zadanému poľu. V tomto príklade používame extend() na pridanie ďalšieho poľa.
Python3 # importing 'array' for array operations import array # initializing array with array values arr1 = array . array ( 'i' [ 1 2 3 1 2 5 ]) arr2 = array . array ( 'i' [ 1 2 3 ]) # using extend() to add array 2 elements to array 1 arr1 . extend ( arr2 ) print ( 'The modified array is : ' ) for i in range ( 0 9 ): print ( arr1 [ i ] end = ' ' )
Výstup
The modified array is : 1 2 3 1 2 5 1 2 3
Funkcia Array fromlist().
Táto funkcia sa používa pripojiť zoznam uvedený v jeho argumente na koniec poľa. V tomto príklade používame fromlist() na pridanie zoznamu na koniec poľa.
Python3 # importing 'array' for array operations import array # initializing array with array values arr = array . array ( 'i' [ 1 2 3 1 2 5 ]) li = [ 1 2 3 ] # using fromlist() to append list at end of array arr . fromlist ( li ) # printing the modified array print ( 'The modified array is : ' end = '' ) for i in range ( 0 9 ): print ( arr [ i ] end = ' ' )
Výstup
The modified array is : 1 2 3 1 2 5 1 2 3
tolist() v Pythone
Táto funkcia sa používa na transformáciu poľa na zoznam. V tomto príklade používame tolist() na konverziu poľa na zoznam.
Python3 # importing 'array' for array operations import array # initializing array with array values arr = array . array ( 'i' [ 1 2 3 1 2 5 ]) # using tolist() to convert array into list li2 = arr . tolist () # printing the new list print ( 'The new list created is : ' end = '' ) for i in range ( 0 len ( li2 )): print ( li2 [ i ] end = ' ' )
Výstup
The new list created is : 1 2 3 1 2 5