Q: What are the contents of L,m after executing the following code? def func (m): for k,v in M.items (): M[k +2] = V+2m = {1:2, 3:4}l = m # copy l[9] = 10func (L) m[ 7] = 8" l: , L) print ( m: " , m)
# above Python version 3.6 will directly error # You cannot modify the size of a list or dictionary when iterating over a list or dictionary! # in Python version 2.6, the results are the same:print l{1:2, 3:4, 5:6, 7:8, 9:10, 11:12< c9>}print m{1:2, 3:4, 5:6, 7:8, 9:10, 11:12}#PS: Older versions of Python will stop Service, so the current version is still relatively good!
Answer
#=, slice, copy, DeepcopyImportCopylist1= [11, 22, [33, 44]]list2=List1list3=List1[:]list4=copy.copy (list1) List4=copy.copy (list1) list5=copy.deepcopy (list1) list1[2].append (55)Print("List2:", List2)#[One, one, [a , a.]]Print("List3:", LIST3)#[One, one, [a , a.]]Print("List4:", LIST4)#[One, one, [a , a.]]Print("LIST5:", LIST5)#[One, one, [+]]#In addition to deepcopy is not a shallow copy, other ways are referenced by the same memory address#and Deepcopy alone opens up new memory space.
Python3 Development questions (dictionaries and copies) 5.30