Python3 Development questions (dictionaries and copies) 5.30

Source: Internet
Author: User

   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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.