Cell assignment of a 2-dimensional Matrix in Python, without numpy

2024/10/10 16:20:05

Below is my script, which basically creates a zero matrix of 12x8 filled with 0. Then I want to fill it in, one by one. So lets say column 2 row 0 needs to be 5. How do I do that? The example below shows how I did it and the wrong (for my needs) output:

list_MatrixRow = []
list_Matrix = [] #Not to be confused by what the book calls, optimal alignment score matrixint_NumbOfColumns = 12
int_NumbOfRows = 8for i in range (0, int_NumbOfColumns): # Puts Zeros across the first Rowlist_AlignMatrixRow.append(0)
for i in range (0, int_NumbOfRows):list_AlignMatrix.append(list_AlignMatrixRow) 
#add the list in another list to make matrix of Zeros
#-------------------THE ACTUAL PROBLEMATIC PART; ABOVE IS FINE(It Works)------------list_AlignMatrix[2][0] = 5 
# This is what logically makes sense but here is the output 
# which happens but I don't want (there should be all 0s and 
# only one 5 on the cell [2][0]):[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Answer

Each row points to the same sublist. This is the result of appending the same sublist repeatedly. So when you modify one row, you end up modifying the others.

I would do this:

ncols = 12
nrows = 8
matrix = [[0] * ncols for i in range(nrows)]
matrix[2][0] = 5 

matrix contains:

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

An aside about coding style: It is poor form in Python to include the type of the object in its name. I have chosen to rename int_NumbOfColumns as ncols. If you need something more descriptive use something like column_count. Generally, mixedCase names are to be avoided, while CamelCase is generally used for class names. See PEP 8 -- Style Guide for Python Code for more.

Edit: Since you mentioned that you are new to Python, here's a little more explanation.

This is a list comprehension:

matrix = [[0] * ncols for i in range(nrows)]

It can also be written as a regular for-loop:

matrix = []
for i in range(nrows):matrix.append([0] * ncols)
https://en.xdnf.cn/q/69875.html

Related Q&A

Fill matplotlib subplots by column, not row

By default, matplotlib subplots are filled by row, not by column. To clarify, the commandsplt.subplot(nrows=3, ncols=2, idx=2) plt.subplot(nrows=3, ncols=2, idx=3)first plot into the upper right plot o…

Find the 2nd highest element

In a given array how to find the 2nd, 3rd, 4th, or 5th values? Also if we use themax() function in python what is the order of complexity i.e, associated with this function max()?.def nth_largest(…

pandas data frame - select rows and clear memory?

I have a large pandas dataframe (size = 3 GB):x = read.table(big_table.txt, sep=\t, header=0, index_col=0)Because Im working under memory constraints, I subset the dataframe:rows = calculate_rows() # a…

How do I format a websocket request?

Im trying to create an application in Python that powers a GPIO port when the balance of a Dogecoin address changes. Im using the websocket API here and this websocket client.My code looks like this:fr…

cherrypy and wxpython

Im trying to make a cherrypy application with a wxpython ui. The problem is both libraries use closed loop event handlers. Is there a way for this to work? If I have the wx ui start cherrypy is that g…

What is the logic behind d3.js nice() ticks

I have generated some charts in d3.js. I use the following code to calculate the values to put in my y axis which works like a charm.var s = d3.scale.linear().domain([minValue, maxValue]); var ticks = …

Changing iterable variable during loop

Let it be an iterable element in python. In what cases is a change of it inside a loop over it reflected? Or more straightforward: When does something like this work?it = range(6) for i in it:it.remo…

Are CPython, IronPython, Jython scripts compatible with each other?

I am pretty sure that python scripts will work in all three, but I want to make sure. I have read here and there about editors that can write CPython, Jython, IronPython and I am hoping that I am look…

Python. Print mac address out of 6 byte string

I have mac address in 6 byte string. How would you print it in "human" readable format?Thanks

Cursors with postgres, where is the data stored and how many calls to the DB

Hi I am using psycopg2 for postgres access.I am trying to understand where "cursor" stores the returned rows. Does it store it in the database as a temporary table or is it on the clients en…