IndexError: list index out of range.Line 7
up vote
-1
down vote
favorite
m = [[1,2,3],
[2,1,3],
[5,4,3]]
for i in range(len(m)):
for j in range(len(m)*len(m)):
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j+1]
is_sink = "True"
if i <= (len(m)-1) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j]
is_sink = "True"
if i <= len(m) and j <= ((len(m)*len(m))-1):
m[i][j] <= m[i][j+1]
is_sink = "True"
if i <= (len(m)-1) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j-1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i-1][j+1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i][j-1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i-1][j]
is_sink = "True"
else:
is_sink = "False"
I was trying to compare adjacent cells to show the lowest point on a map. Although, it showing out of range, I am not very sure how to express the range properly. I am sure this code can be reduced but this is the best length I can display at the moment.
python
New contributor
add a comment |
up vote
-1
down vote
favorite
m = [[1,2,3],
[2,1,3],
[5,4,3]]
for i in range(len(m)):
for j in range(len(m)*len(m)):
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j+1]
is_sink = "True"
if i <= (len(m)-1) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j]
is_sink = "True"
if i <= len(m) and j <= ((len(m)*len(m))-1):
m[i][j] <= m[i][j+1]
is_sink = "True"
if i <= (len(m)-1) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j-1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i-1][j+1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i][j-1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i-1][j]
is_sink = "True"
else:
is_sink = "False"
I was trying to compare adjacent cells to show the lowest point on a map. Although, it showing out of range, I am not very sure how to express the range properly. I am sure this code can be reduced but this is the best length I can display at the moment.
python
New contributor
The length of any data type is measured on a scale from 0 to number of elements - 1. Practically, when you reach the third iteration (when J = 2), you try to access m[i+1][j+1] in the first if statement, which will be 3.
– Tiberiu Zulean
Nov 10 at 15:10
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
m = [[1,2,3],
[2,1,3],
[5,4,3]]
for i in range(len(m)):
for j in range(len(m)*len(m)):
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j+1]
is_sink = "True"
if i <= (len(m)-1) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j]
is_sink = "True"
if i <= len(m) and j <= ((len(m)*len(m))-1):
m[i][j] <= m[i][j+1]
is_sink = "True"
if i <= (len(m)-1) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j-1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i-1][j+1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i][j-1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i-1][j]
is_sink = "True"
else:
is_sink = "False"
I was trying to compare adjacent cells to show the lowest point on a map. Although, it showing out of range, I am not very sure how to express the range properly. I am sure this code can be reduced but this is the best length I can display at the moment.
python
New contributor
m = [[1,2,3],
[2,1,3],
[5,4,3]]
for i in range(len(m)):
for j in range(len(m)*len(m)):
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j+1]
is_sink = "True"
if i <= (len(m)-1) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j]
is_sink = "True"
if i <= len(m) and j <= ((len(m)*len(m))-1):
m[i][j] <= m[i][j+1]
is_sink = "True"
if i <= (len(m)-1) and j <= (len(m)*len(m)):
m[i][j] <= m[i+1][j-1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i-1][j+1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i][j-1]
is_sink = "True"
if i <= len(m) and j <= (len(m)*len(m)):
m[i][j] <= m[i-1][j]
is_sink = "True"
else:
is_sink = "False"
I was trying to compare adjacent cells to show the lowest point on a map. Although, it showing out of range, I am not very sure how to express the range properly. I am sure this code can be reduced but this is the best length I can display at the moment.
python
python
New contributor
New contributor
edited Nov 10 at 22:44
quant
1,25511126
1,25511126
New contributor
asked Nov 10 at 14:57
Naafiu Mohammed
31
31
New contributor
New contributor
The length of any data type is measured on a scale from 0 to number of elements - 1. Practically, when you reach the third iteration (when J = 2), you try to access m[i+1][j+1] in the first if statement, which will be 3.
– Tiberiu Zulean
Nov 10 at 15:10
add a comment |
The length of any data type is measured on a scale from 0 to number of elements - 1. Practically, when you reach the third iteration (when J = 2), you try to access m[i+1][j+1] in the first if statement, which will be 3.
– Tiberiu Zulean
Nov 10 at 15:10
The length of any data type is measured on a scale from 0 to number of elements - 1. Practically, when you reach the third iteration (when J = 2), you try to access m[i+1][j+1] in the first if statement, which will be 3.
– Tiberiu Zulean
Nov 10 at 15:10
The length of any data type is measured on a scale from 0 to number of elements - 1. Practically, when you reach the third iteration (when J = 2), you try to access m[i+1][j+1] in the first if statement, which will be 3.
– Tiberiu Zulean
Nov 10 at 15:10
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
If you want to solve the problem of minimum value in a matrix by yourself (without any other imports), you can easily do it using the following:
import random
m = [[1,2,3],[2,1,3],[5,4,3]];
min_value = m[0][0]
for i in range(len(m)):
for j in range(len(m)):
if m[i][j] < min_value:
min_value = m[i][j]
x = random.randint(0, len(m))
y = random.randint(0, len(m))
if m[x][y] == min_value:
is_min = true;
else is_min = false;
print(is_min)
Because you have a square matrix, the length of columns is the same as the length of rows (meaning i and j are both in range of len(m))
To choose a random matrix component, you can compute 2 random numbers representing the row and column in the matrix. Then acces that component and compare it with the lowest point in the matrix calculated earlier.
hey, thanks for your reply, what i am trying to do is find the lowest point then compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location. It returns false if its not the lowest point.
– Naafiu Mohammed
Nov 10 at 15:48
By lowest point you mean the minimum in all the matrix? Also, what do you mean by "compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location."?
– Tiberiu Zulean
Nov 10 at 15:55
sorry i meant m[0][2] for example.
– Naafiu Mohammed
Nov 10 at 16:08
i dont know how to share and edit but i am basically trying to pick any random point and compare it to the "lowest point" if that is the lowest point i want to then return true
– Naafiu Mohammed
Nov 10 at 16:09
I've included more details in my answer above for you on how to do that.
– Tiberiu Zulean
Nov 10 at 16:16
|
show 3 more comments
up vote
0
down vote
If you are not necessarily interested in solving the problem yourself, here is an example using numpy which does that:
import numpy as np
m = np.array([[1,2,3],
[2,1,3],
[5,4,3]])
np.where(m == m.min())
For the issue in your code: j
should go until len(m)
as well (like i
). For checking if the indices are out of the matrix, do not forget that python indexing goes from 0
to n-1
in case you have a list of length n
.
New contributor
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
If you want to solve the problem of minimum value in a matrix by yourself (without any other imports), you can easily do it using the following:
import random
m = [[1,2,3],[2,1,3],[5,4,3]];
min_value = m[0][0]
for i in range(len(m)):
for j in range(len(m)):
if m[i][j] < min_value:
min_value = m[i][j]
x = random.randint(0, len(m))
y = random.randint(0, len(m))
if m[x][y] == min_value:
is_min = true;
else is_min = false;
print(is_min)
Because you have a square matrix, the length of columns is the same as the length of rows (meaning i and j are both in range of len(m))
To choose a random matrix component, you can compute 2 random numbers representing the row and column in the matrix. Then acces that component and compare it with the lowest point in the matrix calculated earlier.
hey, thanks for your reply, what i am trying to do is find the lowest point then compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location. It returns false if its not the lowest point.
– Naafiu Mohammed
Nov 10 at 15:48
By lowest point you mean the minimum in all the matrix? Also, what do you mean by "compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location."?
– Tiberiu Zulean
Nov 10 at 15:55
sorry i meant m[0][2] for example.
– Naafiu Mohammed
Nov 10 at 16:08
i dont know how to share and edit but i am basically trying to pick any random point and compare it to the "lowest point" if that is the lowest point i want to then return true
– Naafiu Mohammed
Nov 10 at 16:09
I've included more details in my answer above for you on how to do that.
– Tiberiu Zulean
Nov 10 at 16:16
|
show 3 more comments
up vote
0
down vote
accepted
If you want to solve the problem of minimum value in a matrix by yourself (without any other imports), you can easily do it using the following:
import random
m = [[1,2,3],[2,1,3],[5,4,3]];
min_value = m[0][0]
for i in range(len(m)):
for j in range(len(m)):
if m[i][j] < min_value:
min_value = m[i][j]
x = random.randint(0, len(m))
y = random.randint(0, len(m))
if m[x][y] == min_value:
is_min = true;
else is_min = false;
print(is_min)
Because you have a square matrix, the length of columns is the same as the length of rows (meaning i and j are both in range of len(m))
To choose a random matrix component, you can compute 2 random numbers representing the row and column in the matrix. Then acces that component and compare it with the lowest point in the matrix calculated earlier.
hey, thanks for your reply, what i am trying to do is find the lowest point then compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location. It returns false if its not the lowest point.
– Naafiu Mohammed
Nov 10 at 15:48
By lowest point you mean the minimum in all the matrix? Also, what do you mean by "compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location."?
– Tiberiu Zulean
Nov 10 at 15:55
sorry i meant m[0][2] for example.
– Naafiu Mohammed
Nov 10 at 16:08
i dont know how to share and edit but i am basically trying to pick any random point and compare it to the "lowest point" if that is the lowest point i want to then return true
– Naafiu Mohammed
Nov 10 at 16:09
I've included more details in my answer above for you on how to do that.
– Tiberiu Zulean
Nov 10 at 16:16
|
show 3 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If you want to solve the problem of minimum value in a matrix by yourself (without any other imports), you can easily do it using the following:
import random
m = [[1,2,3],[2,1,3],[5,4,3]];
min_value = m[0][0]
for i in range(len(m)):
for j in range(len(m)):
if m[i][j] < min_value:
min_value = m[i][j]
x = random.randint(0, len(m))
y = random.randint(0, len(m))
if m[x][y] == min_value:
is_min = true;
else is_min = false;
print(is_min)
Because you have a square matrix, the length of columns is the same as the length of rows (meaning i and j are both in range of len(m))
To choose a random matrix component, you can compute 2 random numbers representing the row and column in the matrix. Then acces that component and compare it with the lowest point in the matrix calculated earlier.
If you want to solve the problem of minimum value in a matrix by yourself (without any other imports), you can easily do it using the following:
import random
m = [[1,2,3],[2,1,3],[5,4,3]];
min_value = m[0][0]
for i in range(len(m)):
for j in range(len(m)):
if m[i][j] < min_value:
min_value = m[i][j]
x = random.randint(0, len(m))
y = random.randint(0, len(m))
if m[x][y] == min_value:
is_min = true;
else is_min = false;
print(is_min)
Because you have a square matrix, the length of columns is the same as the length of rows (meaning i and j are both in range of len(m))
To choose a random matrix component, you can compute 2 random numbers representing the row and column in the matrix. Then acces that component and compare it with the lowest point in the matrix calculated earlier.
edited Nov 10 at 16:16
answered Nov 10 at 15:22
Tiberiu Zulean
14913
14913
hey, thanks for your reply, what i am trying to do is find the lowest point then compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location. It returns false if its not the lowest point.
– Naafiu Mohammed
Nov 10 at 15:48
By lowest point you mean the minimum in all the matrix? Also, what do you mean by "compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location."?
– Tiberiu Zulean
Nov 10 at 15:55
sorry i meant m[0][2] for example.
– Naafiu Mohammed
Nov 10 at 16:08
i dont know how to share and edit but i am basically trying to pick any random point and compare it to the "lowest point" if that is the lowest point i want to then return true
– Naafiu Mohammed
Nov 10 at 16:09
I've included more details in my answer above for you on how to do that.
– Tiberiu Zulean
Nov 10 at 16:16
|
show 3 more comments
hey, thanks for your reply, what i am trying to do is find the lowest point then compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location. It returns false if its not the lowest point.
– Naafiu Mohammed
Nov 10 at 15:48
By lowest point you mean the minimum in all the matrix? Also, what do you mean by "compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location."?
– Tiberiu Zulean
Nov 10 at 15:55
sorry i meant m[0][2] for example.
– Naafiu Mohammed
Nov 10 at 16:08
i dont know how to share and edit but i am basically trying to pick any random point and compare it to the "lowest point" if that is the lowest point i want to then return true
– Naafiu Mohammed
Nov 10 at 16:09
I've included more details in my answer above for you on how to do that.
– Tiberiu Zulean
Nov 10 at 16:16
hey, thanks for your reply, what i am trying to do is find the lowest point then compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location. It returns false if its not the lowest point.
– Naafiu Mohammed
Nov 10 at 15:48
hey, thanks for your reply, what i am trying to do is find the lowest point then compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location. It returns false if its not the lowest point.
– Naafiu Mohammed
Nov 10 at 15:48
By lowest point you mean the minimum in all the matrix? Also, what do you mean by "compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location."?
– Tiberiu Zulean
Nov 10 at 15:55
By lowest point you mean the minimum in all the matrix? Also, what do you mean by "compare all the points in the sublist, then return true or false if lets say a input [0,2] where that is a location."?
– Tiberiu Zulean
Nov 10 at 15:55
sorry i meant m[0][2] for example.
– Naafiu Mohammed
Nov 10 at 16:08
sorry i meant m[0][2] for example.
– Naafiu Mohammed
Nov 10 at 16:08
i dont know how to share and edit but i am basically trying to pick any random point and compare it to the "lowest point" if that is the lowest point i want to then return true
– Naafiu Mohammed
Nov 10 at 16:09
i dont know how to share and edit but i am basically trying to pick any random point and compare it to the "lowest point" if that is the lowest point i want to then return true
– Naafiu Mohammed
Nov 10 at 16:09
I've included more details in my answer above for you on how to do that.
– Tiberiu Zulean
Nov 10 at 16:16
I've included more details in my answer above for you on how to do that.
– Tiberiu Zulean
Nov 10 at 16:16
|
show 3 more comments
up vote
0
down vote
If you are not necessarily interested in solving the problem yourself, here is an example using numpy which does that:
import numpy as np
m = np.array([[1,2,3],
[2,1,3],
[5,4,3]])
np.where(m == m.min())
For the issue in your code: j
should go until len(m)
as well (like i
). For checking if the indices are out of the matrix, do not forget that python indexing goes from 0
to n-1
in case you have a list of length n
.
New contributor
add a comment |
up vote
0
down vote
If you are not necessarily interested in solving the problem yourself, here is an example using numpy which does that:
import numpy as np
m = np.array([[1,2,3],
[2,1,3],
[5,4,3]])
np.where(m == m.min())
For the issue in your code: j
should go until len(m)
as well (like i
). For checking if the indices are out of the matrix, do not forget that python indexing goes from 0
to n-1
in case you have a list of length n
.
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
If you are not necessarily interested in solving the problem yourself, here is an example using numpy which does that:
import numpy as np
m = np.array([[1,2,3],
[2,1,3],
[5,4,3]])
np.where(m == m.min())
For the issue in your code: j
should go until len(m)
as well (like i
). For checking if the indices are out of the matrix, do not forget that python indexing goes from 0
to n-1
in case you have a list of length n
.
New contributor
If you are not necessarily interested in solving the problem yourself, here is an example using numpy which does that:
import numpy as np
m = np.array([[1,2,3],
[2,1,3],
[5,4,3]])
np.where(m == m.min())
For the issue in your code: j
should go until len(m)
as well (like i
). For checking if the indices are out of the matrix, do not forget that python indexing goes from 0
to n-1
in case you have a list of length n
.
New contributor
New contributor
answered Nov 10 at 15:12
zsomko
212
212
New contributor
New contributor
add a comment |
add a comment |
Naafiu Mohammed is a new contributor. Be nice, and check out our Code of Conduct.
Naafiu Mohammed is a new contributor. Be nice, and check out our Code of Conduct.
Naafiu Mohammed is a new contributor. Be nice, and check out our Code of Conduct.
Naafiu Mohammed is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240163%2findexerror-list-index-out-of-range-line-7%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The length of any data type is measured on a scale from 0 to number of elements - 1. Practically, when you reach the third iteration (when J = 2), you try to access m[i+1][j+1] in the first if statement, which will be 3.
– Tiberiu Zulean
Nov 10 at 15:10