J. Clifford Dyer wrote: >> OK... >> >> from the start. >> >> im trying to develop a simple command line application for determining >> the degree of substitution (DS) on a polymer backbone from elemental >> analysis, i.e., the % weights of different elements in the >> monomer-substituent compound ( i want each element to give a result and >> heaviest atoms give the most accurate results). >> >> most basic comp chem programs use input files but i dont know anything >> about file iteration yet and want the program to be as user friendly as >> possible..i.e. command line prompt. GUI would be great but too much for >> me at this stage >> >> at the start of the script i have 2 dictionaries 1) containing every >> atom in the periodic table with associated isotopic average masses 2) >> containing the molecular forumla of the monomer unit...eg for cellulose >> AGU {'C': 6, 'H': 10, 'O': 5}. >> >> the basic steps are >> >> 1. calculate the weight percentage values for each atom in the monomer >> 2. iterate into dictionaries from DS=0 - DS=15 (0.00005 step) the >> projected % values for the monomer plus substituent, for EACH atom in >> the compound. >> 3. find the (local) minimum from each dictionary/atom to give the >> appropriate DS value. >> >> *Note* I have to iterate ALL the values as there is a non-linear >> relationship between % values and DS due to the different atomic weights >> The computer seems to cope with this in about 10 seconds with the above >> parameters and about 8 elements for the iteration step >> >> > > Since you have a parallel structure for each element, consider using a > dictionary with the element names as keys: > > >>> atomicdata = {} > >>> for element in 'C','H','U': > ... atomicdata[element] = getAtomVars(element) > ... > >>> print atomicdata > { 'C': (1, 2), 'H': (4, 5), 'U': (78, 20) } > > The first value of each tuple will be your Xaa, and the second value > will be Xma. Do you really need to keep the names Caa, Cma, Haa, Hma > around? Instead of Caa, you have atomicdata['C'][0] and Cma becomes > atomicdata['C'][1]. Completely unambiguous. A bit more verbose, > perhaps, but you don't have to try to sneak around the back side of the > language to find the data you are looking for. That's very much against > the tao. If you really want the names, nest dicts, but don't try to get > the element name into the keys, because you already have that: > > >>> atomicdata = { 'C': { 'aa': 1, > ... 'ma': 2}, > ... 'H': { 'aa': 4 > ... 'ma': 5}, > ... 'U': { 'aa': 78 > ... 'ma': 20} } > > and to get from there to storing all your data for all however many > steps, change the value of each entry in atomic data from a tuple (or > dict) to a list of tuples (or dicts). > > > >>> atomicdata = { 'C': [ (1,2), (4,6), (7,8), (20,19) ], > ... 'H': [ (5,7), (2,986), (3,4) ] } > >>> atomicdata['H'].append((5,9)) > >>> atomicdata > { 'C': [ (1, 2), (4, 6), (7, 8), (20, 19) ], 'H': [ (5, 7), (2, 986), > (3, 4), (5, 9) ] } > > You can build up those lists with nested for loops. (one tells you which > element you're working on, the other which iteration). > > The indexes of your lists, of course, will not correspond to the DS > values, but to the step number. To get back to the DS number, of > course, let the index number be i, and calculate DS = i * 0.00005 > > That should get you off and running now. Happy pythoning! > > Cheers, > Cliff > Thanks Cliff,
this is what i need, it seems to make much more sense to do this way. I think once i learn to include datastructures within each other im gonna try to make up this 3D 'array' (i think this is a word from C, is there a python equivalent?) of further iterations within each iteration to take into account the excess water. For this i think ill have to add another 100 values onto the values i already have, i.e. 1.8x10**8 entries in total and god knows how many calculations in potentially one datastructure. Could my computer cope with this or should i try a series of refinement iterations? Does anyone knows of a simple way of determining how much processer time it takes to do these calculations? thanks a -- Dr. Alistair King Research Chemist, Laboratory of Organic Chemistry, Department of Chemistry, Faculty of Science P.O. Box 55 (A.I. Virtasen aukio 1) FIN-00014 University of Helsinki Tel. +358 9 191 50392, Mobile +358 (0)50 5279446 Fax +358 9 191 50366 -- http://mail.python.org/mailman/listinfo/python-list