[Tutor] Help with range of months spanning across years
ian douglas
ian.douglas at iandouglas.com
Wed Feb 2 02:30:22 CET 2011
It bugs me that so many people are quick to jump on the "we wont' do
your homework" bandwagon -- I was accused of the same thing when I
posted a question to the list myself. I've been programming
professionally for many years but learning Python in my spare time... I
sent this reply to Sean privately, but frankly I'm so annoyed at the
'homework' replies, I figured I'd just post it here.
I'm still very junior to Python, but this seems to work for me using
recursion. I'm sure there's a much more elegant way of doing this, but
like I said, I'm still pretty new to the language.
def makelist(startmonth,startyear,endmonth,endyear):
mylist = []
if (startyear == endyear):
for month in range (startmonth,endmonth+1):
mylist += [(startyear,month)]
else:
for month in range (startmonth,13):
mylist += [(startyear,month)]
mylist += makelist(1,startyear+1, endmonth, endyear)
return mylist ;
print makelist(8,2009,1,2010)
I'd love to hear any replies from the experts on the list on how to make
this more efficient in Python 'cause I'm still learning myself.
On 02/01/2011 05:14 PM, Elwin Estle wrote:
> --- On Tue, 2/1/11, Sean Carolan<scarolan at gmail.com> wrote:
>
>> 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)]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110201/b400f56d/attachment.html>
More information about the Tutor
mailing list