0

If i am doing some math functions for different variables for example:

a = x - y
b = x**2 - y**2
c = (x-y)**2
d = x + y

How can i find the minimum value out of all the variables. For example:

a = 4
b = 7
c = 3
d = 10

So the minimum value is 3 for c. How can i let my program do this.

  • What have i thought so far:
  • make a list
  • append a,b,c,d in the list
  • sort the list
  • print list[0] as it will be the smallest value.

The problem is if i append a,b,c,d to a list i have to do something like:

lst.append((a,b,c,d))

This makes the list to be - [(4,7,3,10)]

making all the values relating to one index only ( lst[0] )

If possible is there any substitute to do this or any way possible as to how can i find the minimum!

LNG - PYTHON

Thank you

3
  • Do you know the values of the variables? Commented Nov 11, 2015 at 0:39
  • they are random numbers i put in because the math is not the question here. I just want to find the minimum value and what variable it locates to. in this case minimum value = 3 for c. Commented Nov 11, 2015 at 0:44
  • The "determine the values" and the "find the smallest value" problems may not be as easy to separate as you think, particularly if the system is underdetermined, and separating them might not be the best way to solve things. (Also, finding the values is a much harder problem than just finding the smallest one once you know the values.) Commented Nov 11, 2015 at 0:48

4 Answers 4

3

You can find the index of the smallest item like this

>>> L = [4,7,3,10]
>>> min(range(len(L)), key=L.__getitem__)
2

Now you know the index, you can get the actual item too. eg: L[2]

Another way which finds the answer in the form(index, item)

>>> min(enumerate(L), key=lambda x:x[1])
(2, 3)

I think you may be going the wrong way to solving your problem, but it's possible to pull values of variable from the local namespace if you know their names. eg.

>>> a = 4
>>> b = 7
>>> c = 3
>>> d = 10
>>> min(enumerate(['a', 'b', 'c', 'd']), key=lambda x, ns=locals(): ns[x[1]])
(2, 'c')

a better way is to use a dict, so you are not filling your working namespace with these "junk" variables

>>> D = {}
>>> D['a'] = 4
>>> D['b'] = 7
>>> D['c'] = 3
>>> D['d'] = 10
>>> min(D, key=D.get)
'c'
>>> min(D.items(), key=lambda x:x[1])
('c', 3)

You can see that when the correct data structure is used, the amount of code required is much less.

Sign up to request clarification or add additional context in comments.

3 Comments

so considering the example i gave, will the program know that this minimum value ( in my example - 3 ) is from the variable c?
@Luke, once you create a list of ints [a, b, c, d] there isn't a way to identify anymore which variable were used to create the list other than that you know the order they were in when you created the list.
yes, i think dictionary should solve the issue! Thanks
1

If you store the numbers in an list you can use a reduce having a O(n) complexity due the list is not sorted.

numbers = [999, 1111, 222, -1111]
minimum = reduce(lambda mn, candidate: candidate if candidate < mn else mn, numbers[1:], numbers[0])

Comments

0

pack as dictionary, find min value and then find keys that have matching values (possibly more than one minimum)

D = dict(a = 4, b = 7, c = 3, d = 10)
min_val = min(D.values())
for k,v in D.items():
    if v == min_val: print(k)

Comments

0

The buiit-in function min will do the trick. In your example, min(a,b,c,d) will yield 3.

1 Comment

well i technically want C also. So the program should know minimum value was 3 for c.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.