Find the min and max of each nested list in a list











up vote
0
down vote

favorite












I am trying to find the min and max of each list in a nested list, and the index of when the min or max occurred:



So that, for example:



l=[[5,6,7][6,10,9,6][2,3,1]]


Becomes:



maxl=[7,10,3]
indexl=[2,1,2]


I've tried this and it seems to get me the max list (no indexes yet) but not for min- does anyone know how best to do this?



maxHap=
for subL in happiness1:
maxHap.append(max(subL))
print(maxHap)

minHap=
for subL in happiness1:
minHap.append(min(subL))
print(minHap)


Thank you from a newbie










share|improve this question
























  • Are you open to using an additional dependency like numpy?
    – Mad Physicist
    Nov 11 at 10:27






  • 1




    This should actually work. What results do you get?
    – MrLeeh
    Nov 11 at 10:28










  • The max list is filled but the min list just returns a list of spaces, which is incorrect
    – I. Evans
    Nov 11 at 10:29










  • Got it working! But thanks
    – I. Evans
    Nov 11 at 10:32










  • Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
    – quant
    Nov 11 at 10:33















up vote
0
down vote

favorite












I am trying to find the min and max of each list in a nested list, and the index of when the min or max occurred:



So that, for example:



l=[[5,6,7][6,10,9,6][2,3,1]]


Becomes:



maxl=[7,10,3]
indexl=[2,1,2]


I've tried this and it seems to get me the max list (no indexes yet) but not for min- does anyone know how best to do this?



maxHap=
for subL in happiness1:
maxHap.append(max(subL))
print(maxHap)

minHap=
for subL in happiness1:
minHap.append(min(subL))
print(minHap)


Thank you from a newbie










share|improve this question
























  • Are you open to using an additional dependency like numpy?
    – Mad Physicist
    Nov 11 at 10:27






  • 1




    This should actually work. What results do you get?
    – MrLeeh
    Nov 11 at 10:28










  • The max list is filled but the min list just returns a list of spaces, which is incorrect
    – I. Evans
    Nov 11 at 10:29










  • Got it working! But thanks
    – I. Evans
    Nov 11 at 10:32










  • Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
    – quant
    Nov 11 at 10:33













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to find the min and max of each list in a nested list, and the index of when the min or max occurred:



So that, for example:



l=[[5,6,7][6,10,9,6][2,3,1]]


Becomes:



maxl=[7,10,3]
indexl=[2,1,2]


I've tried this and it seems to get me the max list (no indexes yet) but not for min- does anyone know how best to do this?



maxHap=
for subL in happiness1:
maxHap.append(max(subL))
print(maxHap)

minHap=
for subL in happiness1:
minHap.append(min(subL))
print(minHap)


Thank you from a newbie










share|improve this question















I am trying to find the min and max of each list in a nested list, and the index of when the min or max occurred:



So that, for example:



l=[[5,6,7][6,10,9,6][2,3,1]]


Becomes:



maxl=[7,10,3]
indexl=[2,1,2]


I've tried this and it seems to get me the max list (no indexes yet) but not for min- does anyone know how best to do this?



maxHap=
for subL in happiness1:
maxHap.append(max(subL))
print(maxHap)

minHap=
for subL in happiness1:
minHap.append(min(subL))
print(minHap)


Thank you from a newbie







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 10:26









MrLeeh

2,56831528




2,56831528










asked Nov 11 at 10:24









I. Evans

483




483












  • Are you open to using an additional dependency like numpy?
    – Mad Physicist
    Nov 11 at 10:27






  • 1




    This should actually work. What results do you get?
    – MrLeeh
    Nov 11 at 10:28










  • The max list is filled but the min list just returns a list of spaces, which is incorrect
    – I. Evans
    Nov 11 at 10:29










  • Got it working! But thanks
    – I. Evans
    Nov 11 at 10:32










  • Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
    – quant
    Nov 11 at 10:33


















  • Are you open to using an additional dependency like numpy?
    – Mad Physicist
    Nov 11 at 10:27






  • 1




    This should actually work. What results do you get?
    – MrLeeh
    Nov 11 at 10:28










  • The max list is filled but the min list just returns a list of spaces, which is incorrect
    – I. Evans
    Nov 11 at 10:29










  • Got it working! But thanks
    – I. Evans
    Nov 11 at 10:32










  • Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
    – quant
    Nov 11 at 10:33
















Are you open to using an additional dependency like numpy?
– Mad Physicist
Nov 11 at 10:27




Are you open to using an additional dependency like numpy?
– Mad Physicist
Nov 11 at 10:27




1




1




This should actually work. What results do you get?
– MrLeeh
Nov 11 at 10:28




This should actually work. What results do you get?
– MrLeeh
Nov 11 at 10:28












The max list is filled but the min list just returns a list of spaces, which is incorrect
– I. Evans
Nov 11 at 10:29




The max list is filled but the min list just returns a list of spaces, which is incorrect
– I. Evans
Nov 11 at 10:29












Got it working! But thanks
– I. Evans
Nov 11 at 10:32




Got it working! But thanks
– I. Evans
Nov 11 at 10:32












Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
– quant
Nov 11 at 10:33




Why is the expected output for "l=[[5,6,7][6,10,9,6][2,3,1]]", "maxl=[7,10,3] and indexl=[2,1,2]"? Shouldn't it be (7, 10, 3) and (2, 1, 1) instead?
– quant
Nov 11 at 10:33












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










You can use the method index to get the index of a value of a list:
https://docs.python.org/3/tutorial/datastructures.html



maxl = 
indexl =
for inner_list in l:
max_value = max(inner_list)
maxl.append(max(max_value))
indexl.append(inner_list.index(max_value)





share|improve this answer




























    up vote
    2
    down vote













    Other option:



    l=[[5,6,7],[6,10,9,6],[2,3,1]]

    maxs = [ max(s) for s in l ] #=> [7, 10, 3]
    max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
    mins = [ min(s) for s in l ] #=> [5, 6, 1]
    mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]





    Or you can store the results into an array of dict, one liner:

    mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)

    for k in mapp:
    print(k)

    #=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
    #=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
    #=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}





    share|improve this answer






























      up vote
      1
      down vote













      You can use the following one-liner:



      l=[[5,6,7],[6,10,9,6],[2,3,1]]
      maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
      print(maxL) # will output (7, 10, 3)
      print(index) # will output (2, 1, 1)





      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',
        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%2f53247807%2ffind-the-min-and-max-of-each-nested-list-in-a-list%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        You can use the method index to get the index of a value of a list:
        https://docs.python.org/3/tutorial/datastructures.html



        maxl = 
        indexl =
        for inner_list in l:
        max_value = max(inner_list)
        maxl.append(max(max_value))
        indexl.append(inner_list.index(max_value)





        share|improve this answer

























          up vote
          1
          down vote



          accepted










          You can use the method index to get the index of a value of a list:
          https://docs.python.org/3/tutorial/datastructures.html



          maxl = 
          indexl =
          for inner_list in l:
          max_value = max(inner_list)
          maxl.append(max(max_value))
          indexl.append(inner_list.index(max_value)





          share|improve this answer























            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            You can use the method index to get the index of a value of a list:
            https://docs.python.org/3/tutorial/datastructures.html



            maxl = 
            indexl =
            for inner_list in l:
            max_value = max(inner_list)
            maxl.append(max(max_value))
            indexl.append(inner_list.index(max_value)





            share|improve this answer












            You can use the method index to get the index of a value of a list:
            https://docs.python.org/3/tutorial/datastructures.html



            maxl = 
            indexl =
            for inner_list in l:
            max_value = max(inner_list)
            maxl.append(max(max_value))
            indexl.append(inner_list.index(max_value)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 10:35









            ostcar

            763




            763
























                up vote
                2
                down vote













                Other option:



                l=[[5,6,7],[6,10,9,6],[2,3,1]]

                maxs = [ max(s) for s in l ] #=> [7, 10, 3]
                max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
                mins = [ min(s) for s in l ] #=> [5, 6, 1]
                mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]





                Or you can store the results into an array of dict, one liner:

                mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)

                for k in mapp:
                print(k)

                #=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
                #=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
                #=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}





                share|improve this answer



























                  up vote
                  2
                  down vote













                  Other option:



                  l=[[5,6,7],[6,10,9,6],[2,3,1]]

                  maxs = [ max(s) for s in l ] #=> [7, 10, 3]
                  max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
                  mins = [ min(s) for s in l ] #=> [5, 6, 1]
                  mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]





                  Or you can store the results into an array of dict, one liner:

                  mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)

                  for k in mapp:
                  print(k)

                  #=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
                  #=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
                  #=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}





                  share|improve this answer

























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Other option:



                    l=[[5,6,7],[6,10,9,6],[2,3,1]]

                    maxs = [ max(s) for s in l ] #=> [7, 10, 3]
                    max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
                    mins = [ min(s) for s in l ] #=> [5, 6, 1]
                    mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]





                    Or you can store the results into an array of dict, one liner:

                    mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)

                    for k in mapp:
                    print(k)

                    #=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
                    #=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
                    #=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}





                    share|improve this answer














                    Other option:



                    l=[[5,6,7],[6,10,9,6],[2,3,1]]

                    maxs = [ max(s) for s in l ] #=> [7, 10, 3]
                    max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
                    mins = [ min(s) for s in l ] #=> [5, 6, 1]
                    mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]





                    Or you can store the results into an array of dict, one liner:

                    mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)

                    for k in mapp:
                    print(k)

                    #=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
                    #=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
                    #=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 11 at 11:26

























                    answered Nov 11 at 10:53









                    iGian

                    2,7242622




                    2,7242622






















                        up vote
                        1
                        down vote













                        You can use the following one-liner:



                        l=[[5,6,7],[6,10,9,6],[2,3,1]]
                        maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
                        print(maxL) # will output (7, 10, 3)
                        print(index) # will output (2, 1, 1)





                        share|improve this answer

























                          up vote
                          1
                          down vote













                          You can use the following one-liner:



                          l=[[5,6,7],[6,10,9,6],[2,3,1]]
                          maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
                          print(maxL) # will output (7, 10, 3)
                          print(index) # will output (2, 1, 1)





                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            You can use the following one-liner:



                            l=[[5,6,7],[6,10,9,6],[2,3,1]]
                            maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
                            print(maxL) # will output (7, 10, 3)
                            print(index) # will output (2, 1, 1)





                            share|improve this answer












                            You can use the following one-liner:



                            l=[[5,6,7],[6,10,9,6],[2,3,1]]
                            maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
                            print(maxL) # will output (7, 10, 3)
                            print(index) # will output (2, 1, 1)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 11 at 10:35









                            quant

                            1,58411526




                            1,58411526






























                                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.





                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247807%2ffind-the-min-and-max-of-each-nested-list-in-a-list%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

                                Florida Star v. B. J. F.

                                Danny Elfman

                                Lugert, Oklahoma