Checking for two values within a for loop










1














I am trying to utilize a single for loop in Python to



  1. Check for the largest number within a list then print the index it was found



  2. Count the frequency of that largest number within the list.



    for index in range(1, listLength):
    if numbersList[index] > numbersList[largest]:
    largest = index


I have part 1 complete, however, I am unsure how I can then search for the frequency of it within the same for loop.



Thanks for your help.










share|improve this question



















  • 2




    Set the counter to 1 when you find a new largest number, as you do in your condition, increment it if you find a number that is equal to your largest one.
    – Thierry Lathuille
    Nov 12 at 7:08










  • Why you want in single loop ?
    – PIG
    Nov 12 at 7:25















1














I am trying to utilize a single for loop in Python to



  1. Check for the largest number within a list then print the index it was found



  2. Count the frequency of that largest number within the list.



    for index in range(1, listLength):
    if numbersList[index] > numbersList[largest]:
    largest = index


I have part 1 complete, however, I am unsure how I can then search for the frequency of it within the same for loop.



Thanks for your help.










share|improve this question



















  • 2




    Set the counter to 1 when you find a new largest number, as you do in your condition, increment it if you find a number that is equal to your largest one.
    – Thierry Lathuille
    Nov 12 at 7:08










  • Why you want in single loop ?
    – PIG
    Nov 12 at 7:25













1












1








1







I am trying to utilize a single for loop in Python to



  1. Check for the largest number within a list then print the index it was found



  2. Count the frequency of that largest number within the list.



    for index in range(1, listLength):
    if numbersList[index] > numbersList[largest]:
    largest = index


I have part 1 complete, however, I am unsure how I can then search for the frequency of it within the same for loop.



Thanks for your help.










share|improve this question















I am trying to utilize a single for loop in Python to



  1. Check for the largest number within a list then print the index it was found



  2. Count the frequency of that largest number within the list.



    for index in range(1, listLength):
    if numbersList[index] > numbersList[largest]:
    largest = index


I have part 1 complete, however, I am unsure how I can then search for the frequency of it within the same for loop.



Thanks for your help.







python for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 8:47









Poream3387

518214




518214










asked Nov 12 at 7:04









highfocus

62




62







  • 2




    Set the counter to 1 when you find a new largest number, as you do in your condition, increment it if you find a number that is equal to your largest one.
    – Thierry Lathuille
    Nov 12 at 7:08










  • Why you want in single loop ?
    – PIG
    Nov 12 at 7:25












  • 2




    Set the counter to 1 when you find a new largest number, as you do in your condition, increment it if you find a number that is equal to your largest one.
    – Thierry Lathuille
    Nov 12 at 7:08










  • Why you want in single loop ?
    – PIG
    Nov 12 at 7:25







2




2




Set the counter to 1 when you find a new largest number, as you do in your condition, increment it if you find a number that is equal to your largest one.
– Thierry Lathuille
Nov 12 at 7:08




Set the counter to 1 when you find a new largest number, as you do in your condition, increment it if you find a number that is equal to your largest one.
– Thierry Lathuille
Nov 12 at 7:08












Why you want in single loop ?
– PIG
Nov 12 at 7:25




Why you want in single loop ?
– PIG
Nov 12 at 7:25












5 Answers
5






active

oldest

votes


















1














You don't have to use the loop at all to achieve this. Sort the list and find the number at the last index and use count(number) to get the number of occurences.



numbers = [1, 3, 4, 2, 3, 4, 5]
numbers.sort()
print(numbers)
[1, 2, 3, 3, 4, 4, 5]


list.count(number)



[1, 2, 3, 3, 4, 4, 5].count(5)
1


or you can use import the below one



 from collections import Counter
Counter(list)


will give you the output count in dictionary format.



Counter('1': 1, '2': 1, '3': 2, '4': 2, '5': 1)





share|improve this answer




























    1














    There are few ways around this



    a = [1, 9, 3, 7, 5, 9, 7, 4, 9, 5] # test_data



    1. max() and count



      largest = max(a)
      print(largest) # 9
      print(a.count(largest)) # 3


      Usually, above is the most common and widely used way of getting largest number and counting elements in the list.




    2. max() and Counter



      from collections import Counter
      c = Counter(a)
      print(c[max(a)]) # 3


      Counter is from collections and it supports various kinds of methods. For more, please refer to this website: https://data-flair.training/blogs/python-counter/







    share|improve this answer




























      0














      There are inbuilt methods to do these.



      In [110]: lst = [1,2,4,5,66,7,7,4,66,78,1]
      In [111]: max(lst)
      Out[111]: 78


      1.Check for the largest number within a list then print the index it was found,



      In [112]: print lst.index(max(lst))
      Out[112]: 9


      2.Count the frequency of that largest number within the list.



      In [113]: print lst.count(max(lst))
      Out[113]: 1





      share|improve this answer




























        0














        If you are not allowed to use built-in functions then, take an integer e.g Count. This represents the frequency of the largest number. Go through the array once and when you find a new largest number set largest = index and counter = 0. Add a condition that if the current number is equal to your saved largest number then increment the count by 1.






        share|improve this answer






























          0














          If you don't want to use any libraries, you can achieve it by using below code:



          counter = 1
          for index in range(listLength):
          if index == 0:
          largest = 0
          if numbersList[index] == numbersList[largest]:
          counter += 1
          elif numbersList[index] > numbersList[largest]:
          counter = 1
          largest = index





          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%2f53257294%2fchecking-for-two-values-within-a-for-loop%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You don't have to use the loop at all to achieve this. Sort the list and find the number at the last index and use count(number) to get the number of occurences.



            numbers = [1, 3, 4, 2, 3, 4, 5]
            numbers.sort()
            print(numbers)
            [1, 2, 3, 3, 4, 4, 5]


            list.count(number)



            [1, 2, 3, 3, 4, 4, 5].count(5)
            1


            or you can use import the below one



             from collections import Counter
            Counter(list)


            will give you the output count in dictionary format.



            Counter('1': 1, '2': 1, '3': 2, '4': 2, '5': 1)





            share|improve this answer

























              1














              You don't have to use the loop at all to achieve this. Sort the list and find the number at the last index and use count(number) to get the number of occurences.



              numbers = [1, 3, 4, 2, 3, 4, 5]
              numbers.sort()
              print(numbers)
              [1, 2, 3, 3, 4, 4, 5]


              list.count(number)



              [1, 2, 3, 3, 4, 4, 5].count(5)
              1


              or you can use import the below one



               from collections import Counter
              Counter(list)


              will give you the output count in dictionary format.



              Counter('1': 1, '2': 1, '3': 2, '4': 2, '5': 1)





              share|improve this answer























                1












                1








                1






                You don't have to use the loop at all to achieve this. Sort the list and find the number at the last index and use count(number) to get the number of occurences.



                numbers = [1, 3, 4, 2, 3, 4, 5]
                numbers.sort()
                print(numbers)
                [1, 2, 3, 3, 4, 4, 5]


                list.count(number)



                [1, 2, 3, 3, 4, 4, 5].count(5)
                1


                or you can use import the below one



                 from collections import Counter
                Counter(list)


                will give you the output count in dictionary format.



                Counter('1': 1, '2': 1, '3': 2, '4': 2, '5': 1)





                share|improve this answer












                You don't have to use the loop at all to achieve this. Sort the list and find the number at the last index and use count(number) to get the number of occurences.



                numbers = [1, 3, 4, 2, 3, 4, 5]
                numbers.sort()
                print(numbers)
                [1, 2, 3, 3, 4, 4, 5]


                list.count(number)



                [1, 2, 3, 3, 4, 4, 5].count(5)
                1


                or you can use import the below one



                 from collections import Counter
                Counter(list)


                will give you the output count in dictionary format.



                Counter('1': 1, '2': 1, '3': 2, '4': 2, '5': 1)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 7:17









                Sekar Ramu

                1649




                1649























                    1














                    There are few ways around this



                    a = [1, 9, 3, 7, 5, 9, 7, 4, 9, 5] # test_data



                    1. max() and count



                      largest = max(a)
                      print(largest) # 9
                      print(a.count(largest)) # 3


                      Usually, above is the most common and widely used way of getting largest number and counting elements in the list.




                    2. max() and Counter



                      from collections import Counter
                      c = Counter(a)
                      print(c[max(a)]) # 3


                      Counter is from collections and it supports various kinds of methods. For more, please refer to this website: https://data-flair.training/blogs/python-counter/







                    share|improve this answer

























                      1














                      There are few ways around this



                      a = [1, 9, 3, 7, 5, 9, 7, 4, 9, 5] # test_data



                      1. max() and count



                        largest = max(a)
                        print(largest) # 9
                        print(a.count(largest)) # 3


                        Usually, above is the most common and widely used way of getting largest number and counting elements in the list.




                      2. max() and Counter



                        from collections import Counter
                        c = Counter(a)
                        print(c[max(a)]) # 3


                        Counter is from collections and it supports various kinds of methods. For more, please refer to this website: https://data-flair.training/blogs/python-counter/







                      share|improve this answer























                        1












                        1








                        1






                        There are few ways around this



                        a = [1, 9, 3, 7, 5, 9, 7, 4, 9, 5] # test_data



                        1. max() and count



                          largest = max(a)
                          print(largest) # 9
                          print(a.count(largest)) # 3


                          Usually, above is the most common and widely used way of getting largest number and counting elements in the list.




                        2. max() and Counter



                          from collections import Counter
                          c = Counter(a)
                          print(c[max(a)]) # 3


                          Counter is from collections and it supports various kinds of methods. For more, please refer to this website: https://data-flair.training/blogs/python-counter/







                        share|improve this answer












                        There are few ways around this



                        a = [1, 9, 3, 7, 5, 9, 7, 4, 9, 5] # test_data



                        1. max() and count



                          largest = max(a)
                          print(largest) # 9
                          print(a.count(largest)) # 3


                          Usually, above is the most common and widely used way of getting largest number and counting elements in the list.




                        2. max() and Counter



                          from collections import Counter
                          c = Counter(a)
                          print(c[max(a)]) # 3


                          Counter is from collections and it supports various kinds of methods. For more, please refer to this website: https://data-flair.training/blogs/python-counter/








                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 12 at 7:40









                        Poream3387

                        518214




                        518214





















                            0














                            There are inbuilt methods to do these.



                            In [110]: lst = [1,2,4,5,66,7,7,4,66,78,1]
                            In [111]: max(lst)
                            Out[111]: 78


                            1.Check for the largest number within a list then print the index it was found,



                            In [112]: print lst.index(max(lst))
                            Out[112]: 9


                            2.Count the frequency of that largest number within the list.



                            In [113]: print lst.count(max(lst))
                            Out[113]: 1





                            share|improve this answer

























                              0














                              There are inbuilt methods to do these.



                              In [110]: lst = [1,2,4,5,66,7,7,4,66,78,1]
                              In [111]: max(lst)
                              Out[111]: 78


                              1.Check for the largest number within a list then print the index it was found,



                              In [112]: print lst.index(max(lst))
                              Out[112]: 9


                              2.Count the frequency of that largest number within the list.



                              In [113]: print lst.count(max(lst))
                              Out[113]: 1





                              share|improve this answer























                                0












                                0








                                0






                                There are inbuilt methods to do these.



                                In [110]: lst = [1,2,4,5,66,7,7,4,66,78,1]
                                In [111]: max(lst)
                                Out[111]: 78


                                1.Check for the largest number within a list then print the index it was found,



                                In [112]: print lst.index(max(lst))
                                Out[112]: 9


                                2.Count the frequency of that largest number within the list.



                                In [113]: print lst.count(max(lst))
                                Out[113]: 1





                                share|improve this answer












                                There are inbuilt methods to do these.



                                In [110]: lst = [1,2,4,5,66,7,7,4,66,78,1]
                                In [111]: max(lst)
                                Out[111]: 78


                                1.Check for the largest number within a list then print the index it was found,



                                In [112]: print lst.index(max(lst))
                                Out[112]: 9


                                2.Count the frequency of that largest number within the list.



                                In [113]: print lst.count(max(lst))
                                Out[113]: 1






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 12 at 7:12









                                Rahul K P

                                7,08121833




                                7,08121833





















                                    0














                                    If you are not allowed to use built-in functions then, take an integer e.g Count. This represents the frequency of the largest number. Go through the array once and when you find a new largest number set largest = index and counter = 0. Add a condition that if the current number is equal to your saved largest number then increment the count by 1.






                                    share|improve this answer



























                                      0














                                      If you are not allowed to use built-in functions then, take an integer e.g Count. This represents the frequency of the largest number. Go through the array once and when you find a new largest number set largest = index and counter = 0. Add a condition that if the current number is equal to your saved largest number then increment the count by 1.






                                      share|improve this answer

























                                        0












                                        0








                                        0






                                        If you are not allowed to use built-in functions then, take an integer e.g Count. This represents the frequency of the largest number. Go through the array once and when you find a new largest number set largest = index and counter = 0. Add a condition that if the current number is equal to your saved largest number then increment the count by 1.






                                        share|improve this answer














                                        If you are not allowed to use built-in functions then, take an integer e.g Count. This represents the frequency of the largest number. Go through the array once and when you find a new largest number set largest = index and counter = 0. Add a condition that if the current number is equal to your saved largest number then increment the count by 1.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Nov 12 at 7:22









                                        Vineeth Sai

                                        2,37641023




                                        2,37641023










                                        answered Nov 12 at 7:18









                                        Ch. Waqar Ali

                                        11




                                        11





















                                            0














                                            If you don't want to use any libraries, you can achieve it by using below code:



                                            counter = 1
                                            for index in range(listLength):
                                            if index == 0:
                                            largest = 0
                                            if numbersList[index] == numbersList[largest]:
                                            counter += 1
                                            elif numbersList[index] > numbersList[largest]:
                                            counter = 1
                                            largest = index





                                            share|improve this answer



























                                              0














                                              If you don't want to use any libraries, you can achieve it by using below code:



                                              counter = 1
                                              for index in range(listLength):
                                              if index == 0:
                                              largest = 0
                                              if numbersList[index] == numbersList[largest]:
                                              counter += 1
                                              elif numbersList[index] > numbersList[largest]:
                                              counter = 1
                                              largest = index





                                              share|improve this answer

























                                                0












                                                0








                                                0






                                                If you don't want to use any libraries, you can achieve it by using below code:



                                                counter = 1
                                                for index in range(listLength):
                                                if index == 0:
                                                largest = 0
                                                if numbersList[index] == numbersList[largest]:
                                                counter += 1
                                                elif numbersList[index] > numbersList[largest]:
                                                counter = 1
                                                largest = index





                                                share|improve this answer














                                                If you don't want to use any libraries, you can achieve it by using below code:



                                                counter = 1
                                                for index in range(listLength):
                                                if index == 0:
                                                largest = 0
                                                if numbersList[index] == numbersList[largest]:
                                                counter += 1
                                                elif numbersList[index] > numbersList[largest]:
                                                counter = 1
                                                largest = index






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 12 at 9:09

























                                                answered Nov 12 at 7:30









                                                Sociopath

                                                3,41771635




                                                3,41771635



























                                                    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%2f53257294%2fchecking-for-two-values-within-a-for-loop%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







                                                    這個網誌中的熱門文章

                                                    Barbados

                                                    How to read a connectionString WITH PROVIDER in .NET Core?

                                                    Node.js Script on GitHub Pages or Amazon S3