Tableau en Python | Ensemble 2 (fonctions importantes)

Tableau en Python | Ensemble 1 (Introduction et fonctions)

Tableau en Python | Ensemble 2

Vous trouverez ci-dessous quelques fonctions plus utiles fournies en Python pour les tableaux :

Fonction de code de type de tableau

Cette fonction renvoie le type de données par lequel le tableau est initialisé. Dans cet exemple, nous utilisons arr.typecode pour connaître le type de données d'initialisation du tableau.

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  )   

Sortir
The datatype of array is : i 

Fonction de taille d'élément de tableau

Cette fonction renvoie le taille en octets d'un s élément de tableau unique. Dans cet exemple, nous utilisons la fonction itemsize pour connaître la taille en octets d'un élément du tableau.

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  )   

Sortir
The itemsize of array is : 4 

tampon_info() en Python

Renvoie un tuple représentant le adresse dans laquelle le tableau est stocké et nombre d'éléments qu'il contient. Dans cet exemple, nous utilisons buffer_info() pour faire de même.

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

Sortir
The buffer info. of array is : (140491260368688 6) 

compte() en Python

Nombre Python() la fonction compte le nombre d'occurrences d'argument mentionné dans le tableau.

étendre() en Python

Cette fonction ajoute un tableau entier mentionné dans ses arguments au tableau spécifié. Dans cet exemple, nous utilisons extend() pour ajouter un autre tableau.

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  =  ' '  )   

Sortir
The modified array is : 1 2 3 1 2 5 1 2 3  

Fonction Tableau fromlist()

Cette fonction est utilisée pour ajouter une liste mentionnée dans son argument à la fin du tableau. Dans cet exemple, nous utilisons fromlist() pour ajouter une liste à la fin du tableau.

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  =  ' '  )   

Sortir
The modified array is : 1 2 3 1 2 5 1 2 3  

tolist() en Python

Cette fonction permet de transformer un tableau en liste. Dans cet exemple, nous utilisons tolist() pour convertir un tableau en liste.

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  =  ' '  )   

Sortir
The new list created is : 1 2 3 1 2 5