views:

70

answers:

2

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

+2  A: 

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.

Jesse Dhillon
You really don't need to work with the indexes at all.
Aaron Gallagher
The author asked how to find pvap and xvap for the minimum value they found (and its corresponding `darr` index). You do, indeed, need to work with the indexes from those lists in order to find them again. :)
Andrew
thanks for the explanation, helped a lot :)but `minindex = darr.index(mini)` returns an error; apparently `'dict' object has no attribute 'index'`
pjehyun
I updated it, it's `darr.keys().index()`. Still, I don't know why you need that information anymore. It's especially useless to know the index of a value in `dict.keys()`, but in general indices are not necessary to track if you design your Python program correctly.
Jesse Dhillon
@dhillon ur right, i dont.... im just befuddled by all this new syntax that sometimes i forget the original objective :P. if you dont mind, do you think you could help me over IM or whatever? I know it might be a little late and too much to ask, not to mention a serious risk of internet security on both of our behalfs, but im not like that.
pjehyun
Sorry, I am pretty busy in general. The most help I can give is to answer the occasional question on SO. Besides, I think the best learning comes from doing. You can also try the #python channel on irc.freenode.org Information here: http://www.python.org/community/irc/
Jesse Dhillon
@dhillon ok thanks, i guess it would be too rude to ask for more time than given. but your answer is what i am using so ill give this the answer :)
pjehyun
It's not rude, I just don't have the time. Try the IRC channel, it's pretty useful.
Jesse Dhillon
A: 

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(...)
pillmuncher
could you please explain what you did there? I am a very beginner with programming in general :)
pjehyun
Sorry, missundestood the whole thing.
pillmuncher