In[18]:dict={'id':0}In[19]:q=[]In[20]:foriinrange(3):....:q.append(dict)....:q[i]['id']=i....:printq....:[{'id':0}][{'id':1},{'id':1}][{'id':2},{'id':2},{'id':2}]In[21]:printq[{'id':2},{'id':2},{'id':2}]In[22]:我想的序列q最后应该是[{'id':0},{'id':1},{'id':2}]为什么会得出[{'id':2},{'id':2},{'id':2}]?求解释啊自己的欠缺啊,对象传参是对对象的引用In[5]:importcopyIn[6]:dict={'id':1}In[7]:list=[]In[8]:foriinrange(3):...:list.append(copy.deepcopy(dict))...:list[i]['id']=i...:printlist...:[{'id':0}][{'id':0},{'id':1}][{'id':0},{'id':1},{'id':2}]采用深复制解决问题
长风秋雁
交互式爱情
相关分类