[Tutor] complex iteration over a list (original) (raw)
Gregor Lingl glingl at aon.at
Fri Jul 2 14:52:54 EDT 2004
- Previous message: [Tutor] complex iteration over a list
- Next message: [Tutor] Difference between a class & module?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Michele Alzetta schrieb:
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):
if day <= months[index]:_ _daysinperiod.append(day)_ _if day > months[index]: daysinperiod.append(day-months[index]) Suggestion: Within your for-loop: create a pair of names d, i which initially have the values day and index replace the 2 if-statements with a while loop that calculates the correct day number: you have to subtract months[i] from d as long as d > months[i] and subsequently increment i by 1 (to use the next month in the next execution of the loop body) append d to daysinperiod
Does this help? Regards, Gregor
- Previous message: [Tutor] complex iteration over a list
- Next message: [Tutor] Difference between a class & module?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]