[Tutor] matx3.py

ThreeBlindQuarks threesomequarks at proton.me
Wed Dec 27 23:21:31 EST 2023


That is interesting code but it is far from clear if it does what we thought you wanted.

First, you create a tuple containing a list of lists.

>>> mat = [[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]
>>> mat
([[1], [2], [3]], [[4], [5], [6]], [[7], [8], [9]])
>>> type(mat)
<class 'tuple'>

My guess is you did not mean to provide two lists (with contents) with a comma between them. 

The interpreter saw
mat = stuff, stuff

What you wanted was probably this:

>>> mat = [[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]]
>>> type(mat)
<class 'list'>
>>> len(mat)
3
>>> mat[1]
[[4], [5], [6]]
>>> mat[0]
[[1], [2], [3]]
>>> mat[2]
[[7], [8], [9]]

The extra external brackets make this a list. 

So the rest of the code will not work well until you fix the data structure.

The purpose of doing this SEEMS to be to initialize the data structure to be used later and the entries chosen are meant to be over-written. If properly done, this would be a reasonable strategy but reading on, maybe not.

This code may not do what you want.

 for i in range(0,2):
      for j in range(0,2):
          for k in range(0,8):
              mat[k] = i*j

You are going out of range because of the way you are assigning. mat has THREE elements. Each of those has three inner elements. But you are trying to write to the fourth and more positions in mat and they simply do not exist.

Here is how you address inner parts and I need to mention you did something unexpected as the deepest entries in mat are not atomic but lists with one entry.

>>> mat[0]
[[1], [2], [3]]
>>> mat[0][0]
[1]
>>> mat[0][1]
[2]
>>> mat[0][2]
[3]
>>> mat[2][2]
[9]

To get an actual entry you need a third level as in:

>>> mat[2][2][0]
9

What you asked in the loop thus makes no real sense as you want to write to a top-level item which would simply replace much of your data structure with a smaller one.

You have been offered multiple working ideas and ignored them while displaying examples that suggest you are missing some basics. 

And, I think your approach is flawed even if done right. There is no need for a k if you do it right.

Have fun.





Sent with Proton Mail secure email.

On Wednesday, December 27th, 2023 at 5:34 PM, Ethan Rosenberg <ethanrosenberg414 at gmail.com> wrote:


> Tutor -
> 
> Thank you for the wonderful help you provide to Python programmers.
> 
> Here is the program
> 
> #matx3.py
> #Put ij in cells of 3x3 matrix
> 
> Row = 1
> Column =1
> mat = [[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]
> #print(mat)
> for i in range(0,3):
> print(mat[i])
> for i in range(0,2):
> for j in range(0,2):
> for k in range(0,8):
> mat[k] = ij
> #print(mat)
> for i in range(0,3):
> print(mat[i])
> 
> I receive an error that mat[k] = i*j is a tuple??
> 
> Thanks in advance for your help.
> 
> Ethan Rosenberg
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list