Find number of times a number is followed by a larger number in a list












1














I have this function to find the number of times a number is followed by a larger number in a list. Is there another more "pythonic" way this could be done? I am using Python 3.7.0.



Thanks in advance.



def find_greater_numbers(arr):
count = 0
i = 0
j = 1
while i < len(arr):
while j < len(arr):
if arr[j] > arr[i]:
count += 1
j+=1
j = i+1
i+=1
return count

find_greater_numbers([6,1,2,7]]) # returns 4









share|improve this question





























    1














    I have this function to find the number of times a number is followed by a larger number in a list. Is there another more "pythonic" way this could be done? I am using Python 3.7.0.



    Thanks in advance.



    def find_greater_numbers(arr):
    count = 0
    i = 0
    j = 1
    while i < len(arr):
    while j < len(arr):
    if arr[j] > arr[i]:
    count += 1
    j+=1
    j = i+1
    i+=1
    return count

    find_greater_numbers([6,1,2,7]]) # returns 4









    share|improve this question



























      1












      1








      1







      I have this function to find the number of times a number is followed by a larger number in a list. Is there another more "pythonic" way this could be done? I am using Python 3.7.0.



      Thanks in advance.



      def find_greater_numbers(arr):
      count = 0
      i = 0
      j = 1
      while i < len(arr):
      while j < len(arr):
      if arr[j] > arr[i]:
      count += 1
      j+=1
      j = i+1
      i+=1
      return count

      find_greater_numbers([6,1,2,7]]) # returns 4









      share|improve this question















      I have this function to find the number of times a number is followed by a larger number in a list. Is there another more "pythonic" way this could be done? I am using Python 3.7.0.



      Thanks in advance.



      def find_greater_numbers(arr):
      count = 0
      i = 0
      j = 1
      while i < len(arr):
      while j < len(arr):
      if arr[j] > arr[i]:
      count += 1
      j+=1
      j = i+1
      i+=1
      return count

      find_greater_numbers([6,1,2,7]]) # returns 4






      python python-3.7






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 3:58









      Julien

      7,40531537




      7,40531537










      asked Nov 13 '18 at 3:43









      johnsmithoptionaljohnsmithoptional

      82




      82
























          2 Answers
          2






          active

          oldest

          votes


















          5














          It's a bit unclear if you mean immediately followed, or followed at any later index



          In the first case, this one liner:



          sum(x < y for x,y in zip(arr[:-1],arr[1:])) # answer is 2


          In the second, this one:



          sum(any(x < y for y in arr[i:]) for i,x in enumerate(arr)) # answer is 3


          And if you want to count the number of exact such pairs (like what your actual code seems to be doing):



          sum(x < y for i,x in enumerate(arr) for y in arr[i:]) # answer is 4





          share|improve this answer























          • Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
            – johnsmithoptional
            Nov 13 '18 at 4:24





















          0














          Use map to get the list of True/False for each pair of number in the list depending on the condition if x < y. This list is being generated using the for loop. After getting the list, filter the number of 'True' in the list. Then create a single list using itertools.chain.from_iterable() function and find its length.



          import itertools

          arr = [6, 1, 2, 7]
          num = len(list(itertools.chain.from_iterable(list(filter(lambda x: x , map(lambda x, y: x<y, arr[:-i], arr[i:]))) for i, x in enumerate(arr))))
          print(num)





          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%2f53273467%2ffind-number-of-times-a-number-is-followed-by-a-larger-number-in-a-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            It's a bit unclear if you mean immediately followed, or followed at any later index



            In the first case, this one liner:



            sum(x < y for x,y in zip(arr[:-1],arr[1:])) # answer is 2


            In the second, this one:



            sum(any(x < y for y in arr[i:]) for i,x in enumerate(arr)) # answer is 3


            And if you want to count the number of exact such pairs (like what your actual code seems to be doing):



            sum(x < y for i,x in enumerate(arr) for y in arr[i:]) # answer is 4





            share|improve this answer























            • Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
              – johnsmithoptional
              Nov 13 '18 at 4:24


















            5














            It's a bit unclear if you mean immediately followed, or followed at any later index



            In the first case, this one liner:



            sum(x < y for x,y in zip(arr[:-1],arr[1:])) # answer is 2


            In the second, this one:



            sum(any(x < y for y in arr[i:]) for i,x in enumerate(arr)) # answer is 3


            And if you want to count the number of exact such pairs (like what your actual code seems to be doing):



            sum(x < y for i,x in enumerate(arr) for y in arr[i:]) # answer is 4





            share|improve this answer























            • Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
              – johnsmithoptional
              Nov 13 '18 at 4:24
















            5












            5








            5






            It's a bit unclear if you mean immediately followed, or followed at any later index



            In the first case, this one liner:



            sum(x < y for x,y in zip(arr[:-1],arr[1:])) # answer is 2


            In the second, this one:



            sum(any(x < y for y in arr[i:]) for i,x in enumerate(arr)) # answer is 3


            And if you want to count the number of exact such pairs (like what your actual code seems to be doing):



            sum(x < y for i,x in enumerate(arr) for y in arr[i:]) # answer is 4





            share|improve this answer














            It's a bit unclear if you mean immediately followed, or followed at any later index



            In the first case, this one liner:



            sum(x < y for x,y in zip(arr[:-1],arr[1:])) # answer is 2


            In the second, this one:



            sum(any(x < y for y in arr[i:]) for i,x in enumerate(arr)) # answer is 3


            And if you want to count the number of exact such pairs (like what your actual code seems to be doing):



            sum(x < y for i,x in enumerate(arr) for y in arr[i:]) # answer is 4






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 4:07

























            answered Nov 13 '18 at 3:53









            JulienJulien

            7,40531537




            7,40531537












            • Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
              – johnsmithoptional
              Nov 13 '18 at 4:24




















            • Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
              – johnsmithoptional
              Nov 13 '18 at 4:24


















            Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
            – johnsmithoptional
            Nov 13 '18 at 4:24






            Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
            – johnsmithoptional
            Nov 13 '18 at 4:24















            0














            Use map to get the list of True/False for each pair of number in the list depending on the condition if x < y. This list is being generated using the for loop. After getting the list, filter the number of 'True' in the list. Then create a single list using itertools.chain.from_iterable() function and find its length.



            import itertools

            arr = [6, 1, 2, 7]
            num = len(list(itertools.chain.from_iterable(list(filter(lambda x: x , map(lambda x, y: x<y, arr[:-i], arr[i:]))) for i, x in enumerate(arr))))
            print(num)





            share|improve this answer


























              0














              Use map to get the list of True/False for each pair of number in the list depending on the condition if x < y. This list is being generated using the for loop. After getting the list, filter the number of 'True' in the list. Then create a single list using itertools.chain.from_iterable() function and find its length.



              import itertools

              arr = [6, 1, 2, 7]
              num = len(list(itertools.chain.from_iterable(list(filter(lambda x: x , map(lambda x, y: x<y, arr[:-i], arr[i:]))) for i, x in enumerate(arr))))
              print(num)





              share|improve this answer
























                0












                0








                0






                Use map to get the list of True/False for each pair of number in the list depending on the condition if x < y. This list is being generated using the for loop. After getting the list, filter the number of 'True' in the list. Then create a single list using itertools.chain.from_iterable() function and find its length.



                import itertools

                arr = [6, 1, 2, 7]
                num = len(list(itertools.chain.from_iterable(list(filter(lambda x: x , map(lambda x, y: x<y, arr[:-i], arr[i:]))) for i, x in enumerate(arr))))
                print(num)





                share|improve this answer












                Use map to get the list of True/False for each pair of number in the list depending on the condition if x < y. This list is being generated using the for loop. After getting the list, filter the number of 'True' in the list. Then create a single list using itertools.chain.from_iterable() function and find its length.



                import itertools

                arr = [6, 1, 2, 7]
                num = len(list(itertools.chain.from_iterable(list(filter(lambda x: x , map(lambda x, y: x<y, arr[:-i], arr[i:]))) for i, x in enumerate(arr))))
                print(num)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 4:33









                Rishabh MishraRishabh Mishra

                368210




                368210






























                    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%2f53273467%2ffind-number-of-times-a-number-is-followed-by-a-larger-number-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







                    這個網誌中的熱門文章

                    Hercules Kyvelos

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud