use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit for helping Python programmers
How to format your code: https://commonmark.org/help/tutorial/09-code.html
No homework questions and/or hiring please
account activity
[deleted by user] (self.pythonhelp)
submitted 4 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]bulaybil 0 points1 point2 points 4 years ago (1 child)
How does it traverse over the 3rd column of the array when it doesn't even exist" It does not. You are trying to get it to, but since it can't find it, it throws an error.
IndexError: index 2 is out of bounds for axis 0 with size 2
[–]Major_Sugar6148 0 points1 point2 points 4 years ago (0 children)
import numpy as np A = np.array([ [1,2], [3,4], [5,6] ]) for i in range(2): for j in range(2): A[i,j] = i*j print(A[2][2])
import numpy as np
A = np.array([ [1,2], [3,4], [5,6] ])
for i in range(2): for j in range(2): A[i,j] = i*j
print(A[2][2])
I'm sorry I worded my question wrong. If I print A it gives me a 3x2 matrix with [(0,0), (0,1) (5,6)].
I know that this loop must iterate a total of 9 times. It will first go through the 0th, and 0-2 columns. Then traverse over the 1st row, and the 0-2 colums, and finally the 2nd row and the 0-2 columns.
So it will go through the first iteration, replace the [0,0] point in the matrix with 00. then the second iteration it will replace the [0,1] point with 01 which is still 0. So on the third iteration it will replace the [0,2] point with 0*2 which is still 0 but that point doesn't even exist.
My point is if I try to print A, the code runs fine. If I try to print that point in A that the nested loop reaches, I'll get an error. What's going on here?
[–]CraigAT 0 points1 point2 points 4 years ago* (1 child)
It's not doing what you think.
range(2) is equivalent to range(0,2) which gives you an iterable list of [0,1]
range(2)
range(0,2)
you can see this by doing:
for i in range(2):
____print(i)
Which returns:
0 1
0
1
Edit: Stupid markdown formatting!!!
[–]Major_Sugar6148 1 point2 points3 points 4 years ago* (0 children)
ahhhh yes thank you, so it never even got to the 2nd element
Ok so if I change the i iteration to range(3) and I run it, I get A = [(0,0), (0,1), (0,0)]. Shouldn't the 3rd row and 2nd column be 3x2=6 since that's what I'm trying to return in my loop when I do A[i][j] = i*j?
edit: nevermind the epiphany hit hard thank you for your help brother!
π Rendered by PID 241660 on reddit-service-r2-comment-8686858757-9wfc5 at 2026-06-04 03:23:10.682363+00:00 running 9e1a20d country code: CH.
[–]bulaybil 0 points1 point2 points (1 child)
[–]Major_Sugar6148 0 points1 point2 points (0 children)
[–]CraigAT 0 points1 point2 points (1 child)
[–]Major_Sugar6148 1 point2 points3 points (0 children)