logo

Matriz en Python | Conjunto 2 (funciones importantes)

Matriz en Python | Conjunto 1 (Introducción y Funciones)

Matriz en Python | Conjunto 2

A continuación se muestran algunas funciones más útiles proporcionadas en Python para matrices:

Función de código tipográfico de matriz

Esta función devuelve el tipo de datos mediante el cual se inicializa la matriz. En este ejemplo estamos usando arr.typecode para averiguar el tipo de datos de inicialización de la matriz.



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) 

Producción
The datatype of array is : i

Función de tamaño de elemento de matriz

Esta función devuelve el tamaño en bytes de un s elemento de matriz único. En este ejemplo, utilizamos la función itemsize para averiguar el tamaño en bytes de un elemento de matriz.

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) 

Producción
The itemsize of array is : 4

información_búfer() en pitón

Devuelve una tupla que representa el Dirección en la que se almacena la matriz y número de elementos que contiene. En este ejemplo usamos buffer_info() para hacer lo mismo.

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

Producción
The buffer info. of array is : (140491260368688 6)

contar() en Python

Recuento de Python() La función cuenta el número de ocurrencias. del argumento mencionado en la matriz.



extender() en Python

Esta función agrega una matriz completa mencionada en sus argumentos a la matriz especificada. En este ejemplo usamos extend() para agregar otra matriz.

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

Producción
The modified array is : 1 2 3 1 2 5 1 2 3 

Función matriz de lista ()

Esta función se utiliza para agregue una lista mencionada en su argumento al final de la matriz. En este ejemplo usamos fromlist() para agregar una lista al final de la matriz.

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

Producción
The modified array is : 1 2 3 1 2 5 1 2 3 

tolist() en Python

Esta función se utiliza para transformar una matriz en una lista. En este ejemplo estamos usando tolist() para convertir una matriz en una lista.



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

Producción
The new list created is : 1 2 3 1 2 5