[Tutor] 2d list matrix 7 x 75 problem

Jeff Shannon jeffshannon at gmail.com
Sun Mar 6 11:00:52 CET 2005


On Sun, 06 Mar 2005 09:49:43 +0000, Dave S <pythontut at pusspaws.net> wrote:

> I need to generate a list 2d matrix of the kind ...
> 
> [['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['',
> '', '', '', ''], ['', '', '', '', '']]
> 
> except its dimensions need to be 7 x 75. I thought I had it sorted with
> 
> map2 = [ [''] *7 ] *75
> 
> until the coding screwed up & I realised I had 75 references to the same
> list :-(

Try:

map2 = [['']*7 for n in range(75)]

The list comprehension will execute ['']*7 each iteration, creating a
new list instead of just creating new references to the same list.


> Oh PS
> 
> Is there a more elegant solution to
> 
> if string == 'sun' or string == 'mon' or string == 'tue' or string ==
> 'wed' or string == 'thu' or string == 'fri' or string == 'sat':

Try:

if string in ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']:

(Or perhaps look into using the datetime module, depending on how
detailed your needs are.)

Jeff Shannon


More information about the Tutor mailing list