1

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
SMc
  • 15
  • 1
  • 1
  • 6
  • 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). – Nathan Mar 03 '19 at 00: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 – SMc Mar 03 '19 at 01:01
  • Try: `[[str('00')] * 6] * 3` if you need '00' or else : `[[0] * 6] * 3` – hacker315 Mar 03 '19 at 01:14
  • See related question [How to number a 2 dimensional list?](https://stackoverflow.com/questions/54917081/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). – martineau Mar 03 '19 at 01: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. – fountainhead Mar 03 '19 at 01:22
  • @Nathan: Your first three questions to OP are all valid questions. IMHO, the fourth question "Should the inner lists be rows or columns?" doesn't make sense. – fountainhead Mar 03 '19 at 01:25
  • @martineau thank you sir. I will look at that link – SMc Mar 03 '19 at 01:26
  • @fountainhead if OP has a notion that a matrix has 3 rows and 6 columns, that could translate to either a list of 3 sublists each of which has 6 elements or a list of 6 sublists each of which has 3 elements. I chose to go with 3 sublists, as did you, but who is to say that way is more correct? It just depends on how you want to read it. – Nathan Mar 03 '19 at 01:29
  • SMc: OK, but _don't_ do it the way shown in the question, which is wrong—pay attention to the answers. For example, I think you could use what's in my answer by replacing the `(j*cols+i)` part with `0`. – martineau Mar 03 '19 at 01:33

2 Answers2

5

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.

fountainhead
  • 3,584
  • 1
  • 8
  • 17
  • 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. – SMc Mar 03 '19 at 01:24
  • Isn't this doing the same thing as my suggestion that you warned about?? Just curious.. – hacker315 Mar 03 '19 at 01:28
  • @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. – Nathan Mar 03 '19 at 01:30
  • @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. – fountainhead Mar 03 '19 at 01:31
1

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.

Nathan
  • 9,651
  • 4
  • 45
  • 65
  • Thank you. Will practice using that function and add the constants to see how it works. – SMc Mar 03 '19 at 01:20
  • 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. – Nathan Mar 03 '19 at 01:22