Matriu en Python | Set 2
A continuació es mostren algunes funcions més útils proporcionades a Python per a matrius:
Funció de codi de tipus de matriu
Aquesta funció retorna el tipus de dades amb el qual s'inicialitza la matriu. En aquest exemple estem utilitzant arr.typecode per esbrinar el tipus de dades d'inicialització de la matriu.
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)
Sortida
The datatype of array is : i
Array itemize Funció
Aquesta funció retorna el mida en bytes de a s element de matriu únic. En aquest exemple estem utilitzant la funció Itemize per esbrinar la mida en bytes d'un element de matriu.
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)
Sortida
The itemsize of array is : 4
buffer_info() en Python
Retorna una tupla que representa el adreça en què s'emmagatzema la matriu i nombre d'elements que hi ha. En aquest exemple estem utilitzant buffer_info() per fer el mateix.
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())
Sortida
The buffer info. of array is : (140491260368688 6)
count() a Python
recompte de Python () La funció compta el nombre d'ocurrències de l'argument esmentat a la matriu.
extend() a Python
Aquesta funció afegeix una matriu sencera esmentada als seus arguments a la matriu especificada. En aquest exemple estem utilitzant extend() per afegir una altra matriu.
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 (09): print (arr1[i] end=' ')
Sortida
The modified array is : 1 2 3 1 2 5 1 2 3
Array fromlist() Funció
Aquesta funció s'utilitza per afegiu una llista esmentada al seu argument al final de la matriu. En aquest exemple estem utilitzant fromlist() per afegir una llista al final de la matriu.
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 (09): print (arr[i]end=' ')
Sortida
The modified array is : 1 2 3 1 2 5 1 2 3
tolist() a Python
Aquesta funció s'utilitza per transformar una matriu en una llista. En aquest exemple estem utilitzant tolist() per convertir una matriu en llista.
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 (0len(li2)): print (li2[i]end=' ')
Sortida
The new list created is : 1 2 3 1 2 5