How to check if the input is within the first column of the matrix
up vote
1
down vote
favorite
I have created a matrix called rix
.
rix = [[1,2,3],[4,5,6][7,8,9]]
And as an input I have variable put
which is a single list of length 2
which represents the position of the matrix. The part that I'm struggling in, is how do I check that the variable put represents one of the positions in the first column?
python matrix
add a comment |
up vote
1
down vote
favorite
I have created a matrix called rix
.
rix = [[1,2,3],[4,5,6][7,8,9]]
And as an input I have variable put
which is a single list of length 2
which represents the position of the matrix. The part that I'm struggling in, is how do I check that the variable put represents one of the positions in the first column?
python matrix
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have created a matrix called rix
.
rix = [[1,2,3],[4,5,6][7,8,9]]
And as an input I have variable put
which is a single list of length 2
which represents the position of the matrix. The part that I'm struggling in, is how do I check that the variable put represents one of the positions in the first column?
python matrix
I have created a matrix called rix
.
rix = [[1,2,3],[4,5,6][7,8,9]]
And as an input I have variable put
which is a single list of length 2
which represents the position of the matrix. The part that I'm struggling in, is how do I check that the variable put represents one of the positions in the first column?
python matrix
python matrix
edited Nov 12 at 15:43
asked Nov 11 at 19:25
John.Doe
144
144
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
Design your function to be generic; it should take the argument row_n
(row number) that represents which row you want to check its bounds. Check if the row_n
exists in your rix
's list. Then, do a comparison between your put
's second element (that represents the expected row length) with the original row_n
length in the rix
list.
According to your case, you should compare if the put
's second element is less than or equal to the rix
's row_n
length and then return your booleans.
add a comment |
up vote
0
down vote
You use len(rix)
to evaluate the number of rows
and len(rix[0])
to evaluate the number of columns your matrix has
so
if len(rix) <= put[0]:
return false
elif len(rix[0]) <= put[1]:
return false
else:
return true
add a comment |
up vote
0
down vote
You also have to consider negative indices.
def in_first_col(put, list2D):
row_idx, col_idx = put
# return False if we can't get the row
try:
row = list2D[row_idx]
except IndexError:
return False
# return False if row is empty
# return False if col_idx does not refer to first element of row
return row and (col_idx == 0 or col_idx == -len(row))
Demo:
>>> rix = [[1,2,3],[4,5,6],[7,8,9]]
>>> in_first_col([1,1], rix)
>>> False
>>> in_first_col([2,0], rix)
>>> True
>>> in_first_col([5,0], rix)
>>> False
>>> in_first_col([-2,-3], rix)
>>> True
(Also works for rows of different lengths.)
– timgeb
Nov 11 at 19:53
Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
– Andrew Naguib
Nov 11 at 21:07
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Design your function to be generic; it should take the argument row_n
(row number) that represents which row you want to check its bounds. Check if the row_n
exists in your rix
's list. Then, do a comparison between your put
's second element (that represents the expected row length) with the original row_n
length in the rix
list.
According to your case, you should compare if the put
's second element is less than or equal to the rix
's row_n
length and then return your booleans.
add a comment |
up vote
0
down vote
Design your function to be generic; it should take the argument row_n
(row number) that represents which row you want to check its bounds. Check if the row_n
exists in your rix
's list. Then, do a comparison between your put
's second element (that represents the expected row length) with the original row_n
length in the rix
list.
According to your case, you should compare if the put
's second element is less than or equal to the rix
's row_n
length and then return your booleans.
add a comment |
up vote
0
down vote
up vote
0
down vote
Design your function to be generic; it should take the argument row_n
(row number) that represents which row you want to check its bounds. Check if the row_n
exists in your rix
's list. Then, do a comparison between your put
's second element (that represents the expected row length) with the original row_n
length in the rix
list.
According to your case, you should compare if the put
's second element is less than or equal to the rix
's row_n
length and then return your booleans.
Design your function to be generic; it should take the argument row_n
(row number) that represents which row you want to check its bounds. Check if the row_n
exists in your rix
's list. Then, do a comparison between your put
's second element (that represents the expected row length) with the original row_n
length in the rix
list.
According to your case, you should compare if the put
's second element is less than or equal to the rix
's row_n
length and then return your booleans.
answered Nov 11 at 19:35
Andrew Naguib
1,2361420
1,2361420
add a comment |
add a comment |
up vote
0
down vote
You use len(rix)
to evaluate the number of rows
and len(rix[0])
to evaluate the number of columns your matrix has
so
if len(rix) <= put[0]:
return false
elif len(rix[0]) <= put[1]:
return false
else:
return true
add a comment |
up vote
0
down vote
You use len(rix)
to evaluate the number of rows
and len(rix[0])
to evaluate the number of columns your matrix has
so
if len(rix) <= put[0]:
return false
elif len(rix[0]) <= put[1]:
return false
else:
return true
add a comment |
up vote
0
down vote
up vote
0
down vote
You use len(rix)
to evaluate the number of rows
and len(rix[0])
to evaluate the number of columns your matrix has
so
if len(rix) <= put[0]:
return false
elif len(rix[0]) <= put[1]:
return false
else:
return true
You use len(rix)
to evaluate the number of rows
and len(rix[0])
to evaluate the number of columns your matrix has
so
if len(rix) <= put[0]:
return false
elif len(rix[0]) <= put[1]:
return false
else:
return true
edited Nov 11 at 19:44
answered Nov 11 at 19:39
Alejandro Barrera Lozano
11
11
add a comment |
add a comment |
up vote
0
down vote
You also have to consider negative indices.
def in_first_col(put, list2D):
row_idx, col_idx = put
# return False if we can't get the row
try:
row = list2D[row_idx]
except IndexError:
return False
# return False if row is empty
# return False if col_idx does not refer to first element of row
return row and (col_idx == 0 or col_idx == -len(row))
Demo:
>>> rix = [[1,2,3],[4,5,6],[7,8,9]]
>>> in_first_col([1,1], rix)
>>> False
>>> in_first_col([2,0], rix)
>>> True
>>> in_first_col([5,0], rix)
>>> False
>>> in_first_col([-2,-3], rix)
>>> True
(Also works for rows of different lengths.)
– timgeb
Nov 11 at 19:53
Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
– Andrew Naguib
Nov 11 at 21:07
add a comment |
up vote
0
down vote
You also have to consider negative indices.
def in_first_col(put, list2D):
row_idx, col_idx = put
# return False if we can't get the row
try:
row = list2D[row_idx]
except IndexError:
return False
# return False if row is empty
# return False if col_idx does not refer to first element of row
return row and (col_idx == 0 or col_idx == -len(row))
Demo:
>>> rix = [[1,2,3],[4,5,6],[7,8,9]]
>>> in_first_col([1,1], rix)
>>> False
>>> in_first_col([2,0], rix)
>>> True
>>> in_first_col([5,0], rix)
>>> False
>>> in_first_col([-2,-3], rix)
>>> True
(Also works for rows of different lengths.)
– timgeb
Nov 11 at 19:53
Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
– Andrew Naguib
Nov 11 at 21:07
add a comment |
up vote
0
down vote
up vote
0
down vote
You also have to consider negative indices.
def in_first_col(put, list2D):
row_idx, col_idx = put
# return False if we can't get the row
try:
row = list2D[row_idx]
except IndexError:
return False
# return False if row is empty
# return False if col_idx does not refer to first element of row
return row and (col_idx == 0 or col_idx == -len(row))
Demo:
>>> rix = [[1,2,3],[4,5,6],[7,8,9]]
>>> in_first_col([1,1], rix)
>>> False
>>> in_first_col([2,0], rix)
>>> True
>>> in_first_col([5,0], rix)
>>> False
>>> in_first_col([-2,-3], rix)
>>> True
You also have to consider negative indices.
def in_first_col(put, list2D):
row_idx, col_idx = put
# return False if we can't get the row
try:
row = list2D[row_idx]
except IndexError:
return False
# return False if row is empty
# return False if col_idx does not refer to first element of row
return row and (col_idx == 0 or col_idx == -len(row))
Demo:
>>> rix = [[1,2,3],[4,5,6],[7,8,9]]
>>> in_first_col([1,1], rix)
>>> False
>>> in_first_col([2,0], rix)
>>> True
>>> in_first_col([5,0], rix)
>>> False
>>> in_first_col([-2,-3], rix)
>>> True
answered Nov 11 at 19:47
timgeb
47.4k116287
47.4k116287
(Also works for rows of different lengths.)
– timgeb
Nov 11 at 19:53
Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
– Andrew Naguib
Nov 11 at 21:07
add a comment |
(Also works for rows of different lengths.)
– timgeb
Nov 11 at 19:53
Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
– Andrew Naguib
Nov 11 at 21:07
(Also works for rows of different lengths.)
– timgeb
Nov 11 at 19:53
(Also works for rows of different lengths.)
– timgeb
Nov 11 at 19:53
Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
– Andrew Naguib
Nov 11 at 21:07
Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
– Andrew Naguib
Nov 11 at 21:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53252360%2fhow-to-check-if-the-input-is-within-the-first-column-of-the-matrix%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