Matriz em Python | Conjunto 2
Abaixo estão algumas funções mais úteis fornecidas em Python para arrays:
Função de código de tipo de matriz
Esta função retorna o tipo de dados pelo qual o array é inicializado. Neste exemplo, estamos usando arr.typecode para descobrir o tipo de dados de inicialização do array.
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)
Saída
The datatype of array is : i
Função de tamanho de item da matriz
Esta função retorna o tamanho em bytes de um é único elemento da matriz. Neste exemplo, estamos usando a função itemsize para descobrir o tamanho em bytes de um elemento do array.
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)
Saída
The itemsize of array is : 4
buffer_info() em Python
Retorna uma tupla representando o endereço no qual o array está armazenado e o número de elementos nele. Neste exemplo estamos usando buffer_info() para fazer o mesmo.
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())
Saída
The buffer info. of array is : (140491260368688 6)
contar() em Python
Contagem de Python() função conta o número de ocorrências do argumento mencionado na matriz.
estender() em Python
Esta função anexa um array inteiro mencionado em seus argumentos ao array especificado. Neste exemplo estamos usando extend() para acrescentar outro array.
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=' ')
Saída
The modified array is : 1 2 3 1 2 5 1 2 3
Função Array fromlist()
Esta função é usada para anexe uma lista mencionada em seu argumento ao final do array. Neste exemplo, estamos usando fromlist() para anexar uma lista ao final do array.
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=' ')
Saída
The modified array is : 1 2 3 1 2 5 1 2 3
listar() em Python
Esta função é usada para transformar um array em uma lista. Neste exemplo estamos usando tolist() para converter um array em 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=' ')
Saída
The new list created is : 1 2 3 1 2 5