[Tutor] Naming variables

Wiktor Matuszewski pywkm at wukaem.pl
Sun Jan 19 04:13:33 CET 2014


W dniu 2014-01-18 19:20, Pierre Dagenais pisze:
> I wish to fill a list called years[] with a hundred lists called
> year1900[], year1901[], year1902[], ..., year1999[]. That is too much
> typing of course. Any way of doing this in a loop? I've tried stuff like
> ("year" + str(1900)) = [0,0] but nothing works.
> Any solution?

 >>> y = 1900
 >>> exec('year{} = [0, 0]'.format(y))
 >>> year1900
[0, 0]

But I still think, that solution with dict with lists is better.

-- 
Best regards,     Wiktor Matuszewski  |  Python
'py{}@wu{}em.pl'.format('wkm', 'ka')  |  newbie


More information about the Tutor mailing list