开发者

find value of forloop at which event occurred Python

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 开发者_Go百科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:

  1. Try enumerate
  2. Instead of darr being a list, use a dict and store the dvp values as keys, with the xindex and pindex variables as values

Here's the code

for xindex, xvalue in enumerate(xarr):
  darr = {}
  for pindex, pvalue in enumerate(parr):
    dvp = math.fabs(xvalue - pvalue)
    darr[dvp] = {'xindex': xindex, 'pindex': pindex}
  mini = min(darr.keys())
  minix = darr[mini]['xindex']
  minip = darr[mini]['pindex']
  minindex = darr.keys().index(mini)


  print "minimum_index> {0}, is the difference of xarr[{1}] and parr[{2}]".format(minindex, minix, minip)
  darr.clear()

Explanation

The enumerate function allows you to iterate over a list and also receive the index of the item. It is an alternative to your range(100). Notice that I don't have the line where I get the value at index xpre, ppre, this is because the enumerate function gives me both index and value as a tuple.

The most important change, however, is that instead of your darr being a list like this:

[130, 18, 42, 37 ...]

It is now a dictionary like this:

{
  130: {'xindex': 1, 'pindex': 4},
  18: {'xindex': 1, 'pindex': 6},
  43: {'xindex': 1, 'pindex': 9},
  ...
}

So now, instead of just storing the dvp values alone, I am also storing the indices into x and p which generated those dvp values. Now, if I want to know something, say, Which x and p values produce the dvp value of 43? I would do this:

xindex = darr[43]['xindex']
pindex = darr[43]['pindex']
x = xarr[xindex]
p = parr[pindex]

Now x and p are 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.


Edit: This doesn't answer the OP's question:

min_diff, min_idx = min((math.fabs(a - b), i) for i, (a, b) in enumerate(zip(xpre, ppre)

right to left:

zip takes xpre and ppre and makes a tuple of the 1st, 2nd, ... elements respectively, like so:

[ (xpre[0],ppre[0]) , (xpre[1],ppre[1]) , ... ]

enumerate enumerates adds the index by just counting upwards from 0:

[ (0 , (xpre[0],ppre[0]) ) , (1 , (xpre[1],ppre[1]) ) , ... ]

This unpacks each nestet tuple:

for i, (a, b) in ...

i is the index generated by enumerate, a and b are the elements of xarr and parr.

This builds a tuple consisting of a difference and the index:

(math.fabs(a - b), i)

The whole thing inbetween the min(...) is a generator expression. min then finds the minimal value in these values, and the assignment unpacks them:

min_diff, min_idx = min(...)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜