Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I can use functools.reduce or min / max to get min and max of members in a list. But to get both in one pass I need to write a loop:

from functools import reduce

class foo:
    def __init__(self,value): self.value = value
    
x = []
x.append(foo(1))
x.append(foo(2))
x.append(foo(3))

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

print(min_value)
print(max_value)

min_value2 = min(x,key=lambda a: a.value).value
max_value2 = max(x,key=lambda a: a.value).value

print(min_value2)
print(max_value2)

min_value3 = x[0].value
max_value3 = x[0].value
for f in x:
    if f.value < min_value3: min_value3 = f.value
    if f.value > max_value3: max_value3 = f.value
    
print(min_value3)
print(max_value3)

Is it possible to get min and max in one pass without writing a plain loop?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading