#coding:utf-8importrandomclassSymMat(object):def__init__(self,length=4):self.length=lengthself.matrix=[[0]*length]*lengthvalues=range(length*2)v=random.sample(values,length)pprint(self.matrix)print'------------before'self.matrix[1][0:]=vpprint(self.matrix)defpprint(l):foriinl:printism=SymMat(5)输出:[0,0,0,0,0][0,0,0,0,0][0,0,0,0,0][0,0,0,0,0][0,0,0,0,0]------------before[7,9,4,3,5][7,9,4,3,5][7,9,4,3,5][7,9,4,3,5][7,9,4,3,5]仅对列表中第一行赋值,应该仅仅改变一行的,百思不解!问题已解决,*赋值的时候是直接引用过去的,n个列表id相等~~
相关分类