denismfmcmahon at gmail.com
Tue Oct 28 01:36:53 CET 2014
On Mon, 27 Oct 2014 09:01:57 -0700, umatrp at gmail.com wrote:
> I use python 3.4.0 version. In the course of developing / running a
> python program, I have encountered a problem. I have reproduced below a
> simple program to bring it out.
>>>> d = [*3]*4 dd = [1,2,3,4,5,6,7,8,9,10,11,12]
>>>> for i in range(4):
> ... for j in range(3): d[i][j] = dd[i*3+j]
> [[10, 11, 12], [10, 11, 12], [10, 11, 12], [10, 11, 12]]
> d is not transferred to dd as expected?
> Of course I can use 'append' & do my job (less elegantly though).
Not sure if this is elegant or not:
d = [[list(range(1,13))[i*3+j] for j in range(3)] for i in range(4)]
but it seems to be a one-line solution for what you're trying to do.
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list