unutbu
December 11th, 2008, 07:08 PM
img = [['']*4]*4
is not the same as
img = [['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
The first version makes a list of the form
[A,A,A,A]
each A = ['', '', '', ''] and each A is the exact same object.
The second version makes a list of the form
[A,B,C,D]
with each A,B,C,D being distinct objects.
So in the first version, when you alter elements of A, you alter the element in four places.
#!/usr/bin/env python
img = [['']*4]*4
print img
for x in range(4):
for y in range(4):
img[x][y]=str(x)+str(y)
print img
# [['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
# [['00', '', '', ''], ['00', '', '', ''], ['00', '', '', ''], ['00', '', '', '']]
# [['00', '01', '', ''], ['00', '01', '', ''], ['00', '01', '', ''], ['00', '01', '', '']]
# [['00', '01', '02', ''], ['00', '01', '02', ''], ['00', '01', '02', ''], ['00', '01', '02', '']]
# [['00', '01', '02', '03'], ['00', '01', '02', '03'], ['00', '01', '02', '03'], ['00', '01', '02', '03']]
# [['10', '01', '02', '03'], ['10', '01', '02', '03'], ['10', '01', '02', '03'], ['10', '01', '02', '03']]
# [['10', '11', '02', '03'], ['10', '11', '02', '03'], ['10', '11', '02', '03'], ['10', '11', '02', '03']]
# [['10', '11', '12', '03'], ['10', '11', '12', '03'], ['10', '11', '12', '03'], ['10', '11', '12', '03']]
# [['10', '11', '12', '13'], ['10', '11', '12', '13'], ['10', '11', '12', '13'], ['10', '11', '12', '13']]
# [['20', '11', '12', '13'], ['20', '11', '12', '13'], ['20', '11', '12', '13'], ['20', '11', '12', '13']]
# [['20', '21', '12', '13'], ['20', '21', '12', '13'], ['20', '21', '12', '13'], ['20', '21', '12', '13']]
# [['20', '21', '22', '13'], ['20', '21', '22', '13'], ['20', '21', '22', '13'], ['20', '21', '22', '13']]
# [['20', '21', '22', '23'], ['20', '21', '22', '23'], ['20', '21', '22', '23'], ['20', '21', '22', '23']]
# [['30', '21', '22', '23'], ['30', '21', '22', '23'], ['30', '21', '22', '23'], ['30', '21', '22', '23']]
# [['30', '31', '22', '23'], ['30', '31', '22', '23'], ['30', '31', '22', '23'], ['30', '31', '22', '23']]
# [['30', '31', '32', '23'], ['30', '31', '32', '23'], ['30', '31', '32', '23'], ['30', '31', '32', '23']]
# [['30', '31', '32', '33'], ['30', '31', '32', '33'], ['30', '31', '32', '33'], ['30', '31', '32', '33']]
img = [['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
print img
for x in range(4):
for y in range(4):
img[x][y]=str(x)+str(y)
print img
# [['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
# [['00', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
# [['00', '01', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
# [['00', '01', '02', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
# [['00', '01', '02', '03'], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
# [['00', '01', '02', '03'], ['10', '', '', ''], ['', '', '', ''], ['', '', '', '']]
# [['00', '01', '02', '03'], ['10', '11', '', ''], ['', '', '', ''], ['', '', '', '']]
# [['00', '01', '02', '03'], ['10', '11', '12', ''], ['', '', '', ''], ['', '', '', '']]
# [['00', '01', '02', '03'], ['10', '11', '12', '13'], ['', '', '', ''], ['', '', '', '']]
# [['00', '01', '02', '03'], ['10', '11', '12', '13'], ['20', '', '', ''], ['', '', '', '']]
# [['00', '01', '02', '03'], ['10', '11', '12', '13'], ['20', '21', '', ''], ['', '', '', '']]
# [['00', '01', '02', '03'], ['10', '11', '12', '13'], ['20', '21', '22', ''], ['', '', '', '']]
# [['00', '01', '02', '03'], ['10', '11', '12', '13'], ['20', '21', '22', '23'], ['', '', '', '']]
# [['00', '01', '02', '03'], ['10', '11', '12', '13'], ['20', '21', '22', '23'], ['30', '', '', '']]
# [['00', '01', '02', '03'], ['10', '11', '12', '13'], ['20', '21', '22', '23'], ['30', '31', '', '']]
# [['00', '01', '02', '03'], ['10', '11', '12', '13'], ['20', '21', '22', '23'], ['30', '31', '32', '']]
# [['00', '01', '02', '03'], ['10', '11', '12', '13'], ['20', '21', '22', '23'], ['30', '31', '32', '33']]
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.