[Tutor] 2d list matrix 7 x 75 problem

Dave S pythontut at pusspaws.net
Sun Mar 6 11:37:13 CET 2005


Jeff Shannon wrote:

>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
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>
Many thanks
Dave
:-) :-) :-) :-)


More information about the Tutor mailing list