Checking for two values within a for loop
I am trying to utilize a single for
loop in Python to
Check for the largest number within a
list
then print the index it was foundCount 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
add a comment |
I am trying to utilize a single for
loop in Python to
Check for the largest number within a
list
then print the index it was foundCount 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
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
add a comment |
I am trying to utilize a single for
loop in Python to
Check for the largest number within a
list
then print the index it was foundCount 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
I am trying to utilize a single for
loop in Python to
Check for the largest number within a
list
then print the index it was foundCount 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
python for-loop
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
add a comment |
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
add a comment |
5 Answers
5
active
oldest
votes
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)
add a comment |
There are few ways around this
a = [1, 9, 3, 7, 5, 9, 7, 4, 9, 5] # test_data
max()
andcount
largest = max(a)
print(largest) # 9
print(a.count(largest)) # 3Usually, above is the most common and widely used way of getting largest number and counting elements in the list.
max()
andCounter
from collections import Counter
c = Counter(a)
print(c[max(a)]) # 3Counter
is fromcollections
and it supports various kinds of methods. For more, please refer to this website: https://data-flair.training/blogs/python-counter/
add a comment |
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
add a comment |
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.
add a comment |
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
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%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
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 12 at 7:17
Sekar Ramu
1649
1649
add a comment |
add a comment |
There are few ways around this
a = [1, 9, 3, 7, 5, 9, 7, 4, 9, 5] # test_data
max()
andcount
largest = max(a)
print(largest) # 9
print(a.count(largest)) # 3Usually, above is the most common and widely used way of getting largest number and counting elements in the list.
max()
andCounter
from collections import Counter
c = Counter(a)
print(c[max(a)]) # 3Counter
is fromcollections
and it supports various kinds of methods. For more, please refer to this website: https://data-flair.training/blogs/python-counter/
add a comment |
There are few ways around this
a = [1, 9, 3, 7, 5, 9, 7, 4, 9, 5] # test_data
max()
andcount
largest = max(a)
print(largest) # 9
print(a.count(largest)) # 3Usually, above is the most common and widely used way of getting largest number and counting elements in the list.
max()
andCounter
from collections import Counter
c = Counter(a)
print(c[max(a)]) # 3Counter
is fromcollections
and it supports various kinds of methods. For more, please refer to this website: https://data-flair.training/blogs/python-counter/
add a comment |
There are few ways around this
a = [1, 9, 3, 7, 5, 9, 7, 4, 9, 5] # test_data
max()
andcount
largest = max(a)
print(largest) # 9
print(a.count(largest)) # 3Usually, above is the most common and widely used way of getting largest number and counting elements in the list.
max()
andCounter
from collections import Counter
c = Counter(a)
print(c[max(a)]) # 3Counter
is fromcollections
and it supports various kinds of methods. For more, please refer to this website: https://data-flair.training/blogs/python-counter/
There are few ways around this
a = [1, 9, 3, 7, 5, 9, 7, 4, 9, 5] # test_data
max()
andcount
largest = max(a)
print(largest) # 9
print(a.count(largest)) # 3Usually, above is the most common and widely used way of getting largest number and counting elements in the list.
max()
andCounter
from collections import Counter
c = Counter(a)
print(c[max(a)]) # 3Counter
is fromcollections
and it supports various kinds of methods. For more, please refer to this website: https://data-flair.training/blogs/python-counter/
answered Nov 12 at 7:40
Poream3387
518214
518214
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 12 at 7:12
Rahul K P
7,08121833
7,08121833
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 12 at 7:22
Vineeth Sai
2,37641023
2,37641023
answered Nov 12 at 7:18
Ch. Waqar Ali
11
11
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 12 at 9:09
answered Nov 12 at 7:30
Sociopath
3,41771635
3,41771635
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.
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.
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%2f53257294%2fchecking-for-two-values-within-a-for-loop%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
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