noydb wrote: > On May 12, 12:26 pm, John Machin <sjmac...@lexicon.net> wrote: >> On May 13, 1:58 am, Jaime Fernandez del Rio <jaime.f...@gmail.com> >> wrote: >> >> >> >> >> >> > On Tue, May 12, 2009 at 5:02 PM, MRAB <goo...@mrabarnett.plus.com> >> > wrote: >> > > John Machin wrote: >> >> > >> MRAB <google <at> mrabarnett.plus.com> writes: >> >> > >>> Sort the list, passing a function as the 'key' argument. The >> > >>> function should return an integer for the month, eg 0 for 'jan', 1 >> > >>> for 'feb'. If you want to have a different start month then add >> >> > >> and if you don't like what that produces, try subtract :-) >> >> > > Oops! >> >> > >>> the appropriate >> > >>> integer for that month (eg 0 for 'jan', 1 for 'feb') and then >> > >>> modulo 12 to make it wrap around (there are only 12 months in a >> > >>> year), returning the result. >> >> > > Actually, subtract the start month, add 12, and then modulo 12. >> >> > Both on my Linux and my Windows pythons, modulos of negative numbers >> > are properly taken, returning always the correct positive number >> > between 0 and 11. I seem to recall, from my distant past, that Perl >> > took pride on this being a language feature. Anyone knows if that is >> > not the case with python, and so not adding 12 before taking the >> > modulo could result in wrong results in some implementations? >> >> If that happens, it's a >> bug.http://docs.python.org/reference/expressions.html#binary-arithmetic- o... >> >> If you look at function i_divmod() in the 2.x branch's Objects/ >> intobject.c, you'll be reassured to see that it doesn't just take >> whatever C serves up :-)- Hide quoted text - >> >> - Show quoted text - > > Thanks to those who provided suggestions. I ended up using code > similar to what Jaime provided above first -- truly eloquent and > simple, especially compared to my original thoughts of several messy > loops. I knew it could be done way better. Thanks very much Jaime!! > That was a good learning experience for me. > > fairly finished portion of code: > > ordered_raster_list = [] > > pRasters = gp.ListRasters("precip_*", "All") # an enumeration object, > arcgis method > pRast = pRasters.next() > while pRast: > ## month = pRast[-3:] > ## print month > print pRast > ordered_raster_list.append(pRast) > pRast = pRasters.next() > > > print ordered_raster_list #unordered at this point > > # create a dictionary dictating the order of the the precip_<months> > rasters > monthsD = {"precip_jan" : 1, "precip_feb" : 2, "precip_mar" : 3, > "precip_apr" : 4, "precip_may" : 5, "precip_jun" : 6, > "precip_jul" : 7, "precip_aug" : 8, "precip_sep" : 9, > "precip_oct" : 10, "precip_nov" : 11, "precip_dec" : 12} > > # sort the list based on the dictionary > ordered_raster_list.sort(None, lambda x : monthsD[x]) > > print ordered_raster_list #ordered > > start = 2 #user to define, starting month > > ordered_raster_list = ordered_raster_list[start - 1:] + > ordered_raster_list[:start - 1] > > print ordered_raster_list #ordered but starting in the middle, feb in > this case, ending with jan
Hm, if ordered_raster_list is guaranteed to contain one string item for every month the above can be simplified to months = [ 'precip_jan', 'precip_feb', 'precip_mar', 'precip_apr', 'precip_may', 'precip_jun', 'precip_jul', 'precip_aug', 'precip_sep', 'precip_oct', 'precip_nov', 'precip_dec'] start = 2 ordered_raster_list = months[start-1:] + months[:start-1] print ordered_raster_list What am I missing? Peter -- http://mail.python.org/mailman/listinfo/python-list