Getting the Index of the Returned Max or Min Item Using Max()/Min() on a List

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))

Finding the index of the value which is the min or max in Python

This works:

by_index = ([sub_index, list_index] for list_index, list_item in
enumerate(items) for sub_index in list_item[0])
[item[1] for item in sorted(by_index)]

Gives:

[0, 1, 0, 1, 1]

In detail. The generator:

by_index = ([sub_index, list_index] for list_index, list_item in
enumerate(items) for sub_index in list_item[0])
list(by_index)
[[[0, 1], 0], [[2, 20], 0], [[1, 3], 1], [[5, 29], 1], [[50, 500], 1]]

So the only thing needed is sorting and getting only the desired index:

[item[1] for item in sorted(by_index)]

Returning the index of max value in a list considering another list as a allowed index in python

Use the key argument for max:

max(a,key = lambda i: b[i]) #3

How to find the index of the max value in a list for Python?

  • You are trying to find the index of i. It should be list_c[i].

Easy way/Better way is: idx_max = i. (Based on @Matthias comment above.)

  • Use print to print the results and not return. You use return inside functions.
  • Also your code doesn't work if list_c has all negative values because you are setting max_val to 0. You get a 0 always.
list_c = [-14, 7, -9, 2]

max_val = 0
idx_max = 0

for i in range(len(list_c)):
if list_c[i] > max_val:
max_val = list_c[i]
idx_max = i

print(list_c, max_val, idx_max)

[-14, 7, -9, 2] 7 1

How to get min and max value of member in list in one pass?

You could use a tuple as your aggregator. Something like this maybe?

min_value, max_value = reduce(lambda a, b: 
(a[0] if a[0].value < b.value else b, a[1] if a[1].value > b.value else b),
x,
(x[0], x[1]))

The output should be a tuple where the first is the minimum and the second the maximum.

Example in the REPL, demonstrating that the objects requested are returned, and that the values are correct:

>>> class Foo:
... def __init__(self,value): self.value = value
...
>>> ls = [Foo(1), Foo(2), Foo(3)]
>>> min_value, max_value = reduce(lambda a, b: (a[0] if a[0].value < b.value else b, a[1] if a[1].value > b.value else b), ls, (ls[0], ls[1]))
>>> min_value
<__main__.Foo object at 0x10bd20940>
>>> min_value.value
1
>>> max_value.value
3

For what it's worth, though, I think it's a little clearer if you use a helper function. In this way it's easier to think cleanly about what your accumulator is (your Tuple) and how you're doing the comparison and using reduce().

from typing import Tuple
from functools import reduce

class Foo:
def __init__(self, value): self.value = value

def __repr__(self):
return f"{self.value}"

def min_max(accumulator: Tuple[Foo, Foo], element: Foo) -> Tuple[Foo, Foo]:
minimum, maximum = accumulator
return (minimum if minimum.value < element.value else element,
maximum if maximum.value > element.value else element)

ls = [Foo(x) for x in range(0, 4)] # Or however you construct this list
minimum, maximum = reduce(min_max, ls, (ls[0], ls[0]))
print(f"{minimum=} {maximum=}")

Yielding:

minimum=0 maximum=3

from list of lists get max value with index and map index value to list of string with index

update

if you want to index the list based on the index of the max, use:

a = np.array([[9.8894545, 2.12012004, 2.1054542],
[3.19212970, 9.6048260, 3.63252661],
[9.97873928, 3.10315885, 2.12607185],
[5.13391890, 4.53636282, 9.8429458]])

b = ['abc','def','ghi']

df = pd.Series(np.array(b)[a.argmax(axis=1)])

output:

0    abc
1 def
2 abc
3 ghi
dtype: object

as DataFrame:

df_3 = pd.DataFrame({'column_c': np.array(b)[a.argmax(axis=1)]})

output:

  column_c
0 abc
1 def
2 abc
3 ghi

previous answer

IIUC, use:

a = np.array([[9.8894545, 2.12012004, 2.1054542],
[3.19212970, 9.6048260, 3.63252661],
[9.97873928, 3.10315885, 2.12607185],
[5.13391890, 4.53636282, 9.8429458]])

b = ['abc','def','ghi']

df = pd.DataFrame(a.max(axis=0), index=b)

output:

            0
abc 9.978739
def 9.604826
ghi 9.842946


Related Topics



Leave a reply



Submit