F
F
flashdix2020-09-08 21:54:36
Python
flashdix, 2020-09-08 21:54:36

How to determine the variable name passed to min(), max() in python?

Hello!

There are variables:
a = 0
b = 1
c = 2
d = 3

all = min(a, b, c, d)
print all
###############
output 0

How can I understand now that 0 is a variable named a?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
B
bbkmzzzz, 2020-09-08
@bbkmzzzz

You don't need it. You are working with data, not variable names.
Game, but this is "what you need":

a = 0
b = 1
c = 2
d = 3

alls = min(a, b, c, d)

globs = globals().copy()
for i in globs:
  if id(globs[i]) == id(alls):
    print("имя переменной, у которой самый маленький int:", i)
    break

all is a reserved name, don't use it.

U
U235U235, 2020-09-08
@U235U235

d={'a':0, 'b':1, 'c':2, 'd':3}
print(min(d, key=d.get))

S
soremix, 2020-09-08
@SoreMix

min , max , and others only return the minimum value, not the name of the variable with the minimum.
You can simply define your own variable denoting the minimum number
min_num = min(a,b,c,d)

T
Timur Pokrovsky, 2020-09-08
@Makaroshka007

---------------------------------
Caution! Shitcode!
---------------------------------

l = locals()

print([i for i in l if l[i] == 0][0])

F
flashdix, 2020-09-08
@flashdix

So far, nothing has come up, the task is to calculate the minimum number among 4 variables and then use this variable in the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question