Aquí podemos ver tres formas simples de crear una matriz tanto de ceros como de los números que elijamos.
#!/usr/bin/python
#
# CREANDO MATRICES FACIL
# LA PRIMERA MAS SENCILLA E INTUITIVA
matriz1 = []
numero_filas = 3
numero_columnas = 4
for i in range(numero_filas):
matriz1.append([])
for j in range(numero_columnas):
matriz1[i].append(0) # SI QUIERES UNA MATRIZ DE CEROS O LO QUE QUIERAS.
print matriz1
print
# LA SEGUNDA MENOS INTUITIVA PERO MAS EFICIENTE
matriz2 = [None] * numero_filas
for i in range(numero_filas):
matriz2[i] = [2] * numero_columnas
print matriz2
print
# MATRIZ VACIA
matrizvacia = [None] * numero_filas
for i in range(numero_filas):
matrizvacia[i] = [] * numero_columnas
print matrizvacia
print
# VERSION MAS COMPACTA
matriz3 = [range(numero_columnas) for i in range(numero_filas)]
print matriz3
print
# VARIACION DE LA ANTERIOR
matriz4 = [[0] * numero_columnas for i in range(numero_filas)]
print matriz4
print
La salida que nos dará el script será la siguiente:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]
[[], [], []]
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
