0
votes

I have a list a, then I use max(a) to get the max values. Next step I am trying to use enumerate() to get the first position of max values. But it notices that I could only use enumerate() for int lists? How about float lists? Is there any way to get the 1st position of max values in a float(also with int) list? Thanks a lot

a = [1.5, 1.8, 3.1, 4.2, 5.5, 3.2, 4, 2, 1, 5.5, 3, 2.7]
b = max(a)
maxIndex = [i for i, j in enumerate(b) if j == b][0]

Traceback (most recent call last): File "", line 1, in TypeError: 'float' object is not iterable

4
What are you trying to achieve with enumerate(b)? b is a float, not a list. Did you mean enumerate(a)? - BrenBarn
Thanks. I got it. Simple mistake - William

4 Answers

5
votes

Firstly, I think you meant to use enumerate(a) not enumerate(b), as b is just the max float.

Secondly, you can also do:

maxIndex = a.index(b)
2
votes

max(a) returns the single highest value- it doesn't return a list (or anything iterable) at all. So therefore you can't use enumerate on it.

2
votes

One-liner:

maxpos = max(enumerate(a), key=lambda p: p[1])[0]
0
votes

You have a typo

maxIndex = [i for i, j in enumerate(a) if j == b][0]