I have a list of scores, and I'd like to find the highest values in the list (in my example below, it is the value 80).
scores = [60,29,60,43,10,9,80,45,23,80,56,4]
highest_score = 0
for i in scores:
if i >= highest_score:
highest_score = i
print (highest_score, scores.index(highest_score) )
For highest_scores
, it returns [60,60,80,80]
, whereas I'd like to get only the highest value 80
. For scores.index(highest_score)
, it is giving me the indexes [0,0,6,6]
, whereas I'd like to get the indexes of the highest value - that should be [6,6]
.
How can I improve my code to get the desired outcome?
If you're looking for the indexes of the highest values in [60,29,60,43,10,9,80,45,23,80,56,4]
, I think you mean you want: [6, 9]
.
Assuming that you want a "native" implementation of the algorithm without having to resort to the max()
function, and that you want the indexes I recommend you use enumerate(scores)
:
scores = [60,29,60,43,10,9,80,45,23,80,56,4]
highest_score = scores[0]
indexes = []
for i, s in enumerate(scores):
if s > highest_score:
indexes = []
if s >= highest_score:
indexes.append(i)
highest_score = s
print(highest_score, indexes)
Result:
80 [6, 9]
You can find max value by:
max(scores)
Returns 80
.
The mistake you are doing is in the indentation for print statement. It should be outside the loop. Try the below code:
scores = [60, 29, 60, 43, 10, 9, 80, 45, 23, 80, 56, 4]
highest_score = 0
for i in scores:
if i >= highest_score:
highest_score = I
# find all indices of the highest_score
indices = [i for i, x in enumerate(scores) if x == highest_score]
print(highest_score, indices)
I have also written a list comprehension for finding all the indices of the highest score in the list, as your code would only return the index of the first occurrence.
Output:
80 [6, 9]
There are better ways to do it, however since you want to do it using for loops, this should work:
scores = [60,29,60,43,10,9,80,45,23,80,56,4]
highest_score = 0
index_list = []
for i in scores:
if i >= highest_score:
highest_score = i
for i in scores:
if i == highest_score:
index_list.append(scores.index(i))
print (highest_score , index_list )
Output:
>>> 80 [6, 6]
You can try:
scores = [60,29,60,43,10,9,80,45,23,80,56,4]
highest_score = 0
highest_score_index=[]
for index, score in enumerate(scores):
if score > highest_score:
highest_score = score
highest_score_index.clear()
highest_score_index.append(index)
elif score == highest_score:
highest_score_index.append(index)
print (highest_score, highest_score_index )
for single for loop.
Returns: 80 [6, 9]
scores = [60,29,60,43,10,9,80,45,23,80,56,4]
highest_score = None
highest_score_indexes = []
for index, value in enumerate(scores):
if index == 0:
highest_score = value
highest_score_indexes.append(index)
else:
if highest_score > value:
pass
elif highest_score == value:
highest_score_indexes.append(index)
elif highest_score < value:
highest_score = value
highest_score_indexes = [index]
print("heighest score {}".format(highest_score))
print("heighest score indexes {}".format(highest_score_indexes`enter code here`))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.