Pole v Pythonu | Sada 2 (Důležité funkce)

Pole v Pythonu | Sada 1 (úvod a funkce)

Pole v Pythonu | Sada 2

Níže jsou uvedeny některé další užitečné funkce poskytované v Pythonu pro pole:

Funkce typového kódu pole

Tato funkce vrací datový typ, kterým je pole inicializováno. V tomto příkladu používáme arr.typecode ke zjištění datového typu inicializace pole.

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 

Array itemsize Funkce

Tato funkce vrací velikost v bajtech a s jeden prvek pole. V tomto příkladu používáme funkci itemsize ke zjištění velikosti prvku pole v bytech.

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 Pythonu

Vrátí n-tici představující adresa, ve které je pole uloženo, a počet prvků v něm. V tomto příkladu k tomu používáme 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 Pythonu

Python count() funkce počítá počet výskytů argumentu uvedeného v poli.

extend() v Pythonu

Tato funkce připojí celé pole uvedené v jeho argumentech k zadanému poli. V tomto příkladu používáme extend() k připojení dalšího pole.

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  

Funkce pole fromlist().

Tato funkce se používá připojit seznam uvedený v jeho argumentu na konec pole. V tomto příkladu používáme fromlist() k připojení seznamu na konec pole.

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 Pythonu

Tato funkce se používá k transformaci pole na seznam. V tomto příkladu používáme tolist() k převodu pole na seznam.

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