[Tutor] complex iteration over a list (original) (raw)
Michele Alzetta michele.alzetta at aliceposta.it
Fri Jul 2 14:03:04 EDT 2004
- Previous message: [Tutor] Pickling objects and then carrying on development
- Next message: [Tutor] complex iteration over a list
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hallo all !
I'm stuck on the following type of code (still working on classes that handle periods of time).
I have a list called months:
months = [31,28,31,30,31,30,31,31,30,31,30,31] daysinperiod = [] startday, numberofdays, index = 10, 40, 5
actual value of these has been determined previously
in my program, I am just assigning them directly now to
keep things simple
for day in range(startday,numberofdays):
the range is a period of time, in this case, from the 10th
of June to the 20th of July
if day <= months[index]:
daysinperiod.append(day)
if day > months[index]:
daysinperiod.append(day-months[index])print daysinperiod
The output I want is a list like this: 10,11,..,30,1,2,...,19
Above code works perfectly in this case, but it doesn't if numberofdays is > 51 of course. To have this work for periods that overlap the end of two or more months I would have to be able to change the value of "index" in my recursion somehow ... ? For all practical purposes I could simply forbid the case where the length of my time period is too long, but supposing in the future I wanted to adapt this class for other things ? Besides, the purpose of this whole thing is learning !
I could also look into the calendar module... (which will probably end up being the simplest way of handling this). But I keep on feeling there ought to be an elegant way to do this even without calendar. Any suggestions ?
-- Michele Alzetta <michele.alzetta at aliceposta.it>
- Previous message: [Tutor] Pickling objects and then carrying on development
- Next message: [Tutor] complex iteration over a list
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]