[Tutor] Help with range of months spanning across years
Elwin Estle
chrysalis_reborn at yahoo.com
Wed Feb 2 02:14:55 CET 2011
--- On Tue, 2/1/11, Sean Carolan <scarolan at gmail.com> wrote:
> From: Sean Carolan <scarolan at gmail.com>
> Subject: [Tutor] Help with range of months spanning across years
> To: Tutor at python.org
> Date: Tuesday, February 1, 2011, 6:19 PM
> I have a function that accepts four
> arguments, namely startmonth,
> startyear, endmonth, and endyear. For example:
>
> startmonth = 8
> startyear = 2009
> endmonth = 1
> endyear = 2010
>
> What would be the most straightforward way to create a list
> of
> year/month pairs from start to end? I want to end up
> with a list of
> tuples like this:
>
> mylist = [(2009, 8), (2009, 9), (2009, 10), (2009, 11),
> (2009, 12), (2010, 1)]
--- On Tue, 2/1/11, Sean Carolan <scarolan at gmail.com> wrote:
> From: Sean Carolan <scarolan at gmail.com>
> Subject: [Tutor] Help with range of months spanning across years
> To: Tutor at python.org
> Date: Tuesday, February 1, 2011, 6:19 PM
> I have a function that accepts four
> arguments, namely startmonth,
> startyear, endmonth, and endyear. For example:
>
> startmonth = 8
> startyear = 2009
> endmonth = 1
> endyear = 2010
>
> What would be the most straightforward way to create a list
> of
> year/month pairs from start to end? I want to end up
> with a list of
> tuples like this:
>
> mylist = [(2009, 8), (2009, 9), (2009, 10), (2009, 11),
> (2009, 12), (2010, 1)]
Well, I noticed someone else's reply to this, about it potentially being homework, so I won't post any code.
I tinkered with your problem and have a solution, but I didn't use nested for loops as someone else suggested. My solution works, so long as the end year isn't the same as the start year, tho with a bit of fiddling, one could add some conditional code to take care of that.
I would suggest thinking about the situation with the first year/month pair in the date range, and the last year/date...vs any years in the middle. What is different? What is the same?
More information about the Tutor
mailing list