Matrices in Python

1

I have a question with the creation of matrices in Python, specifically with the following code:

def inicializaMatriz(FILAS,COLUMNAS):
    matrizA = []
    for i in range(FILAS):
        a = [0]*COLUMNAS
        matrizA.append(a)
    return matrizA    

This is an implementation that I found, but what is not very clear to me is the line:

a = [0]*COLUMNAS

I have an idea about what he does but someone knows how to interpret that line?

    
asked by Iras 06.08.2016 в 23:58
source

2 answers

1

Responding to your question, the specific code that you ask for creates a list of zeros with the length of COLUMNAS .

Now a recommendation, if you are going to work with matrices / arrays in Python it is best to use a specialized library as numpy .

    
answered by 07.08.2016 / 01:35
source
0

You can use numpy.zeros :

import numpy as np
rows    = 3
columns = 2
matrix =  np.zeros((rows,columns))
print matrix
[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]
    
answered by 13.10.2016 в 22:10