-1

I'm trying to get the index of the biggest number in a part of the list, like that:

if the list is this:

91 71 52 38 17 14 91 43 58 50 27 29 48

I'm doing this with i = 5. (So I would think my code snippet should have to choose between 14 and 91)

i = linelist.index(max(linelist[i:i+2]))

What I would like after my statement is i= 6 (corresponding to the second occurrence of 91) but it keep giving me 0, for the first occurence of 91. Is there anyway to have my code choosing the index in the part of the list I'm feeding to it?

Thanks

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Reine Baudache
  • 413
  • 4
  • 16
  • May be you can check: https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list – niraj Jun 15 '20 at 15:49
  • It might work in a complicated way, because your link points to a solution giving all the possible index position of the number but I really only need the specific one. I guess I might make it work using an if statement but is there nothing more direct? – Reine Baudache Jun 15 '20 at 15:57
  • I still don't get what exactly is being evaluated here, I guess we need more details for pinpointed answering, please explain more – Agent_Orange Jun 15 '20 at 16:03

2 Answers2

2

You can determine the index relative to the slice that you passed to max() and then add the base index of the slice:

slice = linelist[i:i+2]
i = i + slice.index(max(slice))
chash
  • 3,975
  • 13
  • 29
0

Here's the easy way to do it. You are missing the addition of i

x = [91, 71,  52, 38, 17, 14, 91, 43,  58, 50, 27, 29, 48]

for i in range(0, len(x)):
    temp_list = x[i: i+2]
    max_list = max(temp_list)
    max_index = i + temp_list.index(max_list)
    print(max_index)
ParthS007
  • 2,581
  • 1
  • 22
  • 37