Ryan Rich
0
Q:

2d array python3

# Using above first method to create a  
# 2D array 
rows, cols = (5, 5) 
arr = [[0]*cols]*rows 
print(arr) 
4
# Using above second method to create a  
# 2D array 
rows, cols = (5, 5) 
arr = [[0 for i in range(cols)] for j in range(rows)] 
print(arr) 
0
def build_matrix(rows, cols):
    matrix = []

    for r in range(0, rows):
        matrix.append([0 for c in range(0, cols)])

    return matrix

if __name__ == '__main__':
    build_matrix(6, 10)
5
from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
for r in T:
    for c in r:
        print(c,end = " ")
    print()
2
for row in A:
    for val in row:
        print '{:4}'.format(val),
    print
0
# 5x6, 2-d array of booleans using list comprehension:

matrix = [[False for col in range(6)] for row in range(5)]

# 6x5, 2-d array of banana's using list comprehension:

matrix = [['banana' for col in range(5)] for row in range(6)]
0
import numpy as np
print(np.matrix(A))
0

New to Communities?

Join the community