0
 my_list = [1,3,3,8,2,7,8]

 max = my_list[0]

 for i in range(1,len(my_list)):

    if my_list[i] > max:

       max = my_list[i]
print('Max number is :', max)

I wrote a code to find a max value in a list and it is working. Now I have to find its(I mean max values') number of repeated times. I have to change this code in order to find its max value and max values' repeated time. Example it will be 8 and 2. But how to do it I do not know. I am stuck with it. Please help me with it.

ichramm
  • 6,437
  • 19
  • 30
Ziyodbek
  • 1
  • 1

1 Answers1

0
my_list = [1,3,3,8,2,7,8]

max = my_list[0]
count = 1

for i in range(1,len(my_list)):
    if my_list[i] > max:
        max = my_list[i]
        count = 1
    elif my_list[i] == max:
        count += 1
print('Max number is :', max)
print('Count :', count)

yields

Max number is : 8
Count : 2

Some coding style suggestions.

  1. max is a built-in python function. It is considered to be a good idea to not override it. I suggest replacing it with max_element in the code.
  2. There is a more pythonic way to iterate over lists by using for element in list syntax.
my_list = [1,3,3,8,2,7,8]

max_element = my_list[0]
count = 1

for v in my_list[1:]:
    if v > max_element:
        max_element = v
        count = 1
    elif v == max_element:
        count += 1
print('Max number is :', max_element)
print('Count :', count)
Curious
  • 507
  • 3
  • 16