How do I figure out the value which is greater than certain threshold in a matrix?
Assume that I have a matrix:
a = [[4,7,2],[0,1,4],[4,5,6]]
And I want to get
b = [0, 1]
c = [[2],[0,1]]
b = [0,1]
because the inner lists ofa
at position0
and1
contain values that are smaller then3
.
c = [[2],[0,1]]
because the[2]
nd element of the first sublist inb
is below 3 and[0,1]
because the first and second element in the second sublist inb
is below 3.
I tried :
for i in a:
for o in i:
if o < 3:
print(i)
It only returns the original matrix.
How do I get b
&c
in python?
python python-3.x numpy for-loop enumerate
add a comment |
Assume that I have a matrix:
a = [[4,7,2],[0,1,4],[4,5,6]]
And I want to get
b = [0, 1]
c = [[2],[0,1]]
b = [0,1]
because the inner lists ofa
at position0
and1
contain values that are smaller then3
.
c = [[2],[0,1]]
because the[2]
nd element of the first sublist inb
is below 3 and[0,1]
because the first and second element in the second sublist inb
is below 3.
I tried :
for i in a:
for o in i:
if o < 3:
print(i)
It only returns the original matrix.
How do I get b
&c
in python?
python python-3.x numpy for-loop enumerate
1
"Matrix b is that a[0], a[1] have a value that is smaller than 3, c is a[0][2] and a[0][0], a[0][1] is less than 3". This doesn't make the question sound clear at all
– Bazingaa
Jan 1 at 16:04
This doesn't give it in the exact form you are looking for, but depending on why you want to do this, you might be interested innp.where(a<3)
which outputs the tuple([0,1,1],[2,0,1])
giving all the indices wherea<3
.
– tch
Jan 1 at 17:00
add a comment |
Assume that I have a matrix:
a = [[4,7,2],[0,1,4],[4,5,6]]
And I want to get
b = [0, 1]
c = [[2],[0,1]]
b = [0,1]
because the inner lists ofa
at position0
and1
contain values that are smaller then3
.
c = [[2],[0,1]]
because the[2]
nd element of the first sublist inb
is below 3 and[0,1]
because the first and second element in the second sublist inb
is below 3.
I tried :
for i in a:
for o in i:
if o < 3:
print(i)
It only returns the original matrix.
How do I get b
&c
in python?
python python-3.x numpy for-loop enumerate
Assume that I have a matrix:
a = [[4,7,2],[0,1,4],[4,5,6]]
And I want to get
b = [0, 1]
c = [[2],[0,1]]
b = [0,1]
because the inner lists ofa
at position0
and1
contain values that are smaller then3
.
c = [[2],[0,1]]
because the[2]
nd element of the first sublist inb
is below 3 and[0,1]
because the first and second element in the second sublist inb
is below 3.
I tried :
for i in a:
for o in i:
if o < 3:
print(i)
It only returns the original matrix.
How do I get b
&c
in python?
python python-3.x numpy for-loop enumerate
python python-3.x numpy for-loop enumerate
edited Jan 1 at 16:14


Patrick Artner
25.3k62444
25.3k62444
asked Jan 1 at 15:47


wayne64001wayne64001
475
475
1
"Matrix b is that a[0], a[1] have a value that is smaller than 3, c is a[0][2] and a[0][0], a[0][1] is less than 3". This doesn't make the question sound clear at all
– Bazingaa
Jan 1 at 16:04
This doesn't give it in the exact form you are looking for, but depending on why you want to do this, you might be interested innp.where(a<3)
which outputs the tuple([0,1,1],[2,0,1])
giving all the indices wherea<3
.
– tch
Jan 1 at 17:00
add a comment |
1
"Matrix b is that a[0], a[1] have a value that is smaller than 3, c is a[0][2] and a[0][0], a[0][1] is less than 3". This doesn't make the question sound clear at all
– Bazingaa
Jan 1 at 16:04
This doesn't give it in the exact form you are looking for, but depending on why you want to do this, you might be interested innp.where(a<3)
which outputs the tuple([0,1,1],[2,0,1])
giving all the indices wherea<3
.
– tch
Jan 1 at 17:00
1
1
"Matrix b is that a[0], a[1] have a value that is smaller than 3, c is a[0][2] and a[0][0], a[0][1] is less than 3". This doesn't make the question sound clear at all
– Bazingaa
Jan 1 at 16:04
"Matrix b is that a[0], a[1] have a value that is smaller than 3, c is a[0][2] and a[0][0], a[0][1] is less than 3". This doesn't make the question sound clear at all
– Bazingaa
Jan 1 at 16:04
This doesn't give it in the exact form you are looking for, but depending on why you want to do this, you might be interested in
np.where(a<3)
which outputs the tuple ([0,1,1],[2,0,1])
giving all the indices where a<3
.– tch
Jan 1 at 17:00
This doesn't give it in the exact form you are looking for, but depending on why you want to do this, you might be interested in
np.where(a<3)
which outputs the tuple ([0,1,1],[2,0,1])
giving all the indices where a<3
.– tch
Jan 1 at 17:00
add a comment |
1 Answer
1
active
oldest
votes
You can leverate enumerate(iterable[,startingvalue])
which gives you the index and the value of the thing you iterate over:
a = [[4,7,2],[0,1,4],[4,5,6]]
thresh = 3
b = # collects indexes of inner lists with values smaller then thresh
c = # collects indexes in the inner lists that are smaller then thresh
for idx, inner_list in enumerate(a):
if any(value < thresh for value in inner_list):
b.append(idx)
c.append()
for idx_2, value in enumerate(inner_list):
if value < thresh:
c[-1].append(idx_2)
print(a)
print(b)
print(c)
Output:
[[4, 7, 2], [0, 1, 4], [4, 5, 6]]
[0, 1]
[[2], [0, 1]]
Doku:
- enumerate()
- any()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53996793%2fhow-do-i-figure-out-the-value-which-is-greater-than-certain-threshold-in-a-matri%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can leverate enumerate(iterable[,startingvalue])
which gives you the index and the value of the thing you iterate over:
a = [[4,7,2],[0,1,4],[4,5,6]]
thresh = 3
b = # collects indexes of inner lists with values smaller then thresh
c = # collects indexes in the inner lists that are smaller then thresh
for idx, inner_list in enumerate(a):
if any(value < thresh for value in inner_list):
b.append(idx)
c.append()
for idx_2, value in enumerate(inner_list):
if value < thresh:
c[-1].append(idx_2)
print(a)
print(b)
print(c)
Output:
[[4, 7, 2], [0, 1, 4], [4, 5, 6]]
[0, 1]
[[2], [0, 1]]
Doku:
- enumerate()
- any()
add a comment |
You can leverate enumerate(iterable[,startingvalue])
which gives you the index and the value of the thing you iterate over:
a = [[4,7,2],[0,1,4],[4,5,6]]
thresh = 3
b = # collects indexes of inner lists with values smaller then thresh
c = # collects indexes in the inner lists that are smaller then thresh
for idx, inner_list in enumerate(a):
if any(value < thresh for value in inner_list):
b.append(idx)
c.append()
for idx_2, value in enumerate(inner_list):
if value < thresh:
c[-1].append(idx_2)
print(a)
print(b)
print(c)
Output:
[[4, 7, 2], [0, 1, 4], [4, 5, 6]]
[0, 1]
[[2], [0, 1]]
Doku:
- enumerate()
- any()
add a comment |
You can leverate enumerate(iterable[,startingvalue])
which gives you the index and the value of the thing you iterate over:
a = [[4,7,2],[0,1,4],[4,5,6]]
thresh = 3
b = # collects indexes of inner lists with values smaller then thresh
c = # collects indexes in the inner lists that are smaller then thresh
for idx, inner_list in enumerate(a):
if any(value < thresh for value in inner_list):
b.append(idx)
c.append()
for idx_2, value in enumerate(inner_list):
if value < thresh:
c[-1].append(idx_2)
print(a)
print(b)
print(c)
Output:
[[4, 7, 2], [0, 1, 4], [4, 5, 6]]
[0, 1]
[[2], [0, 1]]
Doku:
- enumerate()
- any()
You can leverate enumerate(iterable[,startingvalue])
which gives you the index and the value of the thing you iterate over:
a = [[4,7,2],[0,1,4],[4,5,6]]
thresh = 3
b = # collects indexes of inner lists with values smaller then thresh
c = # collects indexes in the inner lists that are smaller then thresh
for idx, inner_list in enumerate(a):
if any(value < thresh for value in inner_list):
b.append(idx)
c.append()
for idx_2, value in enumerate(inner_list):
if value < thresh:
c[-1].append(idx_2)
print(a)
print(b)
print(c)
Output:
[[4, 7, 2], [0, 1, 4], [4, 5, 6]]
[0, 1]
[[2], [0, 1]]
Doku:
- enumerate()
- any()
answered Jan 1 at 15:56


Patrick ArtnerPatrick Artner
25.3k62444
25.3k62444
add a comment |
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.
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%2f53996793%2fhow-do-i-figure-out-the-value-which-is-greater-than-certain-threshold-in-a-matri%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
1
"Matrix b is that a[0], a[1] have a value that is smaller than 3, c is a[0][2] and a[0][0], a[0][1] is less than 3". This doesn't make the question sound clear at all
– Bazingaa
Jan 1 at 16:04
This doesn't give it in the exact form you are looking for, but depending on why you want to do this, you might be interested in
np.where(a<3)
which outputs the tuple([0,1,1],[2,0,1])
giving all the indices wherea<3
.– tch
Jan 1 at 17:00