How to Find All Positions of the Maximum Value in a List

How to find all positions of the maximum value in a list?

>>> m = max(a)
>>> [i for i, j in enumerate(a) if j == m]
[9, 12]

How to find the position of the maximum value in a list in R?

It's little mess, but it will give you location of maximum value

library(stringr)
library(dplyr)

x <- which.max(unlist(test))
y <- str_split(names(x), "", simplify = T)
a <- y[1]

z <- sapply(test[[a]], length) %>% cumsum
b <- which(z > as.numeric(y[2]))
c <- as.numeric(y[2]) - z[b-1]

test[[a]][[b]][[c]]

[1] 2094234

In this code, a, b, and c indicate location. If you need more generalized version, please let me know

How to find position of big value in python list array

This is pretty simple, but it will give you the index of the first occurrence:

>>> l = [3,5,1,8,9]
>>> l.index(max(l))
4

I strongly suggest you not use the name of built-in functions as list for variables.

Getting the index of the returned max or min item using max()/min() on a list

if is_min_level:
return values.index(min(values))
else:
return values.index(max(values))

Pythonic way to find maximum value and its index in a list?

There are many options, for example:

import operator
index, value = max(enumerate(my_list), key=operator.itemgetter(1))

Get max value from a list with lists?

Loop through your outer list and select the last element of each sublist:

def max_value(inputlist):
return max([sublist[-1] for sublist in inputlist])

print max_value(resultlist)
# 9.1931

It's also best if you keep all function related variables in-scope (pass the list as an argument and don't confuse the namespace by reusing variable names).

Get every n-th element of list starting from maximum value

Finding the first index of your array that you want to keep is simple math: idx % reprate

newy = y[idx%reprate::reprate]


Related Topics



Leave a reply



Submit