Skip to content Skip to sidebar Skip to footer

How Can I Turn Random Matrix Into A Table?

Here is the code I'm given. import random def create_random_matrix(rows_min, rows_max, cols_min, cols_max): matrix = [] # generate a random number for the number of rows

Solution 1:

This is a simple solution to your problem to print the members of a list of lists:

mymatrix = [[52, 23, 11, 95, 79], [3, 63, 11], [5, 78, 3, 14, 37], [89, 98, 10], [24, 60, 80, 73, 84, 94], [45, 14, 28], [51, 19, 9], [43, 86, 63, 71, 19], [58, 6, 43, 17, 87, 64, 87], [77, 57, 97], [9, 71, 54, 20], [77, 86, 22]]for list in mymatrix:
    for item in list:
        print item,
    print

the output would look like:

52 23 11 95 79
3 63 11
5 78 3 14 37
89 98 10
24 60 80 73 84 94
45 14 28
51 19 9
43 86 63 71 19
58 6 43 17 87 64 87
77 57 97
9 71 54 20
77 86 22

Solution 2:

just change the way you print it:

>>>for i in random_matrix:...print" ".join(str(j) for j in i)... 
52 23 11 95 79
3 63 11
5 78 3 14 37
89 98 10
24 60 80 73 84 94
45 14 28
51 19 9
43 86 63 71 19
58 6 43 17 87 64 87
77 57 97
9 71 54 20

And just for fun, in one line:

print"\n".join(" ".join(str(j) for j in i) for i in random_matrix)

Post a Comment for "How Can I Turn Random Matrix Into A Table?"