0

In the Python code below, I am simply assigning OPN_24n4 the minimum value of either Node_23n3 or Node_24n4. However, I also would like to know the name of the variable that was chosen. Is there a concise way of doing this? I suppose I could use some sort of if statement but thought there might be a function I'm unaware of.

OPN_24n4 = min(Node_23n3, Node_23n4)
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 1
    If it's as simple as just two variables, you only need a single if statement`if Node_23n3 > Node_23n4: ... else ...` (as long as you don't really care if they're equal). No need for another function. If you have many more than two, you might be better off using a dictionary of keys and values, rather than separate variables. – David Buck Oct 25 '21 at 22:28
  • 3
    " However, I also would like to know the name of the variable that was chosen. Is there a concise way of doing this?" No, because *a variable was not chosen*. `min` (like any other function you call) *never sees* your variable names, and in fact never sees that the *values* it was given were already named as variables. You can equally well write `min(1, 2)`. – Karl Knechtel Oct 25 '21 at 22:31
  • i feel like if you explain more of the problem you're trying to solve people will be able to show you why you dont need to learn the name of a variable ever – AntiMatterDynamite Oct 25 '21 at 22:36
  • 1
    if you just want to keep track of whether the 1st or 2nd argument was minimum, then you may want a function that returns a tuple that includes the position. `find_min = lambda x, y: (x, 1) if x <= y else (y, 2)` – Rad Oct 25 '21 at 22:53

3 Answers3

2

min, max, and sorted can take a key function, which is a function that manipulate the input in order make the comparison over some part of it or whatever, and we can use this and then make tuples with the name of the variable and its value and with an appropriate key function get the result we desire

>>> min(("var1",10),("var2",2), key=lambda x:x[1])
('var2', 2)
>>> 
Copperfield
  • 8,131
  • 3
  • 23
  • 29
0

If you are working with lists you can get the index of the minimum value with the index method

>>> x = [5,3,7,1,9]
>>> x.index(min(x))
3
>>> 
Malcolm
  • 461
  • 2
  • 10
0

It seems unlikely that you would ever need a solution like this , and it would probably be relatively fragile but, borrowing from this answer you could create a solution like this using inspect:

import inspect

def named_min(*args):
    min_val = min(args)
    callers_local_vars = inspect.currentframe().f_back.f_locals.items()
    named_var = [var_name for var_name, var_val in callers_local_vars if var_val is args[args.index(min_val)]][0]
    if args.count(min_val) > 1:
        named_var = 'multiple'
    return min_val, named_var

Node_23n3 = 0
Node_23n4 = 1
OPN_24n4, name = named_min(Node_23n3, Node_23n4)
print(OPN_24n4, name)
Node_23n4 = 0
OPN_24n4, name = named_min(Node_23n3, Node_23n4)
print(OPN_24n4, name)
Node_23n3 = 2
OPN_24n4, name = named_min(Node_23n3, Node_23n4)
print(OPN_24n4, name)
Node_23n5 = -1
OPN_24n4, name = named_min(Node_23n3, Node_23n4, Node_23n5)
print(OPN_24n4, name)

This does do what you're after:

0 Node_23n3
0 multiple
0 Node_23n4
-1 Node_23n5

but I certainly wouldn't recommend it.

David Buck
  • 3,752
  • 35
  • 31
  • 35