Reputation: 25
Lets say I have the list below:
list1=[1,2,4,6,8,3,2,5,8,4,2]
I want to return the integer, 2, because 8 is the maximum value and there are two 8s in the list. How can I do this? Edit: I also want to assume that the maximum number in the list can be any negative or non-negative number including zero.
Upvotes: 0
Views: 402
Reputation: 1637
Well you can use something like this:
list1=[1,2,4,6,8,3,2,5,8,4,2]
print list1.count(max(list1))
Upvotes: 1
Reputation: 1862
>>> list1=[1,2,4,6,8,3,2,5,8,4,2]
>>> x = max(list1)
>>> l = []
>>> for i in list1:
if i == x:
l.append(i)
>>> l
[8, 8]
>>> len(l)
2
OR
>>> list1=[1,2,4,6,8,3,2,5,8,4,2]
>>> x = max(list1)
>>> result = len(filter(lambda i: i == x, list1))
>>> result
2
Upvotes: 0
Reputation: 1459
ans = 0
mx = 0
for x in list1:
if x > mx:
mx = x
ans = 1
elif x == mx :
ans += 1
print ans
assume max number is bigger than 0, otherwise you should initial mx
with the negative infinity
Upvotes: 0