Create a 2D array with a nested loop?

Note this: >>> matrix = 0 * 3 * 3 >>> x for x in matrix 0, 0, 0, 0, 0, 0, 0, 0, 0 >>> id(x) for x in matrix 32484168, 32484168, 32484168 >>> Three rows but only one object. See the docs especially Note 2 about the s * n operation. Fix: >>> m2= 0 * 3 for I in xrange(5) >>> id(x) for x in m2 32498152, 32484808, 32498192, 32499952, 32499872 >>> Update: Here are some samples of code that gets an answer simply (i.e.

Without iter()): >>> nrows = 2; ncols = 4 >>> zeroes = 0 for j in xrange(ncols) for I in xrange(nrows) >>> zeroes 0, 0, 0, 0, 0, 0, 0, 0 >>> ap = ncols * I + j for j in xrange(ncols) for I in xrange(nrows) >>> ap 0, 1, 2, 3, 4, 5, 6, 7.

1 The docs says "...This often haunts new Python programmers...". True! But why this feature can be useful?

– user949952 Sep 24 at 2:20.

Try running matrix00 = 0 after that. Notice it now becomes: 0, 7, 8, 0, 7, 8, 0, 7, 8 So it's changing all three at the same time. Read this: daniweb.com/software-development/python/... That seems to explain it.

1 Thanks Mk12 for the link. And I have the same question: whether this is a "feature" of Python or a "bug. "?

– user949952 Sep 24 at 2:08 1 It isn't something I expected, that's for sure. But it makes sense. – Mk12 Sep 24 at 2:13 1 @user949952: The docs (see my answer) say that it takes a shallow copy of the sequence.So it's a feature.

– John Machin Sep 24 at 2:14.

"), you're making copies of the same list with multiplication and, therefore, modifying one of them will modify all of them.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions