How do I figure out the value which is greater than certain threshold in a matrix?












-2















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 of a at position 0 and 1 contain values that are smaller then 3.


  • c = [[2],[0,1]] because the [2] nd element of the first sublist in b is below 3 and [0,1] because the first and second element in the second sublist in b 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?










share|improve this question




















  • 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 where a<3.

    – tch
    Jan 1 at 17:00


















-2















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 of a at position 0 and 1 contain values that are smaller then 3.


  • c = [[2],[0,1]] because the [2] nd element of the first sublist in b is below 3 and [0,1] because the first and second element in the second sublist in b 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?










share|improve this question




















  • 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 where a<3.

    – tch
    Jan 1 at 17:00
















-2












-2








-2








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 of a at position 0 and 1 contain values that are smaller then 3.


  • c = [[2],[0,1]] because the [2] nd element of the first sublist in b is below 3 and [0,1] because the first and second element in the second sublist in b 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?










share|improve this question
















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 of a at position 0 and 1 contain values that are smaller then 3.


  • c = [[2],[0,1]] because the [2] nd element of the first sublist in b is below 3 and [0,1] because the first and second element in the second sublist in b 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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 where a<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














1 Answer
1






active

oldest

votes


















0














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()






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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()






    share|improve this answer




























      0














      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()






      share|improve this answer


























        0












        0








        0







        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()






        share|improve this answer













        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()







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 15:56









        Patrick ArtnerPatrick Artner

        25.3k62444




        25.3k62444
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory