hey guys, this is very confusing…
i am trying to find the minimum of an array by:
for xpre in range(100): #used pre because I am using vapor pressures with some x molarity
xvalue=xarray[xpre]
for ppre in range(100): #same as xpre but vapor pressures for pure water, p
pvalue=parray[p]
d=math.fabs(xvalue-pvalue) #d represents the difference(due to vapor pressure lowering, a phenomenon in chemistry)
darray.append(d) #darray stores the differences
mini=min(darray) #mini is the minimum value in darray
darr=[] #this is to make way for a new set of floats
all the arrays (xarr,parr,darr)are already defined and what not. they have 100 floats each
so my question is how would I find the pvap and the xvap @ which min(darr) is found?
edit
have changed some variable names and added variable descriptions, sorry guys
A couple things:
enumeratedarrbeing alist, use adictand store thedvpvalues as keys, with thexindexandpindexvariables as valuesHere’s the code
Explanation
The
enumeratefunction allows you to iterate over a list and also receive the index of the item. It is an alternative to yourrange(100). Notice that I don’t have the line where I get the value at indexxpre,ppre, this is because theenumeratefunction gives me both index and value as a tuple.The most important change, however, is that instead of your
darrbeing a list like this:It is now a dictionary like this:
So now, instead of just storing the
dvpvalues alone, I am also storing the indices intoxandpwhich generated thosedvpvalues. Now, if I want to know something, say, Whichxandpvalues produce thedvpvalue of 43? I would do this:Now
xandpare the values in question.Note I personally would store the values which produced a particular
dvp, and not the indices of those values. But you asked for the indices so I gave you that answer. I’m going to assume that you have a reason for wanting to handle indices like this, but in Python generally you do not find yourself handling indices in this way when you are programming in Pythonic manner. This is a very C way of doing things.