4

Have looked all over but can't seem to find info if not on numpy. Need to create a 3 x 6 zero matrix using python not numpy & for the output to look exactly like this:

Matrix 3 by 6 with 0's:
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
9
  • 1
    When you say matrix, what exactly do you mean? In numpy, that's defined. In Python, there is no "matrix" type. Do you want a list of lists? Or a tuple of tuples? Should the inner lists be rows or columns? Whichever one you pick, it will be easier to access those slices of the matrix than the others (e.g. easier to get a particular row than to get a column). Commented Mar 3, 2019 at 0:53
  • Apologies if I am not using the correct wording. First need to create a two dimensional list to have rows and columns, with the constants of ROWS and COLS 3 and 6 Commented Mar 3, 2019 at 1:01
  • Try: [[str('00')] * 6] * 3 if you need '00' or else : [[0] * 6] * 3 Commented Mar 3, 2019 at 1:14
  • See related question How to number a 2 dimensional list? — your question is essentially a simpler version of it since you want all values to be the same (zero). Commented Mar 3, 2019 at 1:17
  • @hacker315: Your suggestion [[0] * 6] * 3 is what people are usually warned against doing, because, even though it builds an outer list of inner list references, all the inner list references refer to the same inner list object. So, if a modification is made to one row, it is effectively made to all rows. Commented Mar 3, 2019 at 1:22

2 Answers 2

8

This works:

rows, cols = 3,6
my_matrix = [([0]*cols) for i in range(rows)]

Explanation:

  1. The expression ([0]*cols) represents each element of the list built by the list expression (the outer square brackets in [([0]*cols) for i in range(rows)])
  2. That expression ([0]*cols) produces a one-dimensional list of zeros (as many zeros as the value of cols)

So, effectively, the list expression builds a one-dimensional list of lists, where each inner list has cols zeros.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Will also try this and see how it works. I'm wanting to learn MY logical way of seeing how it all works in Python.
Isn't this doing the same thing as my suggestion that you warned about?? Just curious..
@hacker315 no - fountainhead is creating a new list each time, but you were just making multiple references to the same list. Try your approach vs this one out in an interpreter and watch what happens if you modify the first list.
@hacker315: Good question. The answer is no, it isn't the same thing. You can test it out, by writing a small loop, which will print id(my_matrix[cur_row]) for all values of cur_row. You will see that the ids are all different.
2

I assume you know how to make a list in python: my_list = [0, 0, 0]. To make a list of lists, we can just nest list definitions inside of another list: nested_list = [[0, 0, 0], [0, 0, 0]]. Obviously you won't want to write the whole thing out, so we can abstract this into a function:

def make_zeros(n_rows: int, n_columns: int):
    matrix = []
    for i in range(n_rows):
        matrix.append([0] * n_columns)
    return matrix

This makes an outer list and then appends inner lists ("rows") to it one at a time. That was meant to be a very clear definition regardless of your level with Python, but you can shorten this a bit if you want by using list comprehensions:

def make_zeros(n_rows: int, n_columns: int):
    return [[0] * n_columns for _ in range(n_rows)]

Note that the inner lists are meant to be rows here, so you can easily access the rows:

matrix = make_zeros(3, 6)
matrix[0] # the first row

However, the "columns" consist of one element from each row; there isn't a list of these already, so if you wanted to get the first column, you have to do something like

col0 = [row[0] for row in matrix]

You could write a helper function to make this easier, but soon you'd be writing helper functions for all of the common matrix/array operations, and then you're reinventing the wheel that numpy has already made so nicely.

2 Comments

Thank you. Will practice using that function and add the constants to see how it works.
If this answers your question, you can click the check mark on the left of the answer to accept this answer and let others know that your problem has been resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.