Python Common errors

Source: Internet
Author: User

1, single element tuple (1,) instead of (1)

2. Copy of List

>>> mylist=[1,2]>>> mylist2=mylist>>> mylist3=list (mylist) >>> mylist2[1]=3>>> mylist2[1, 3]>>> mylist[1, 3] >>> mylist3[1, 2]

It is obvious here that Mylist2 is just a reference to mylist, so modifying the mylist2 is actually modifying the MyList

Mylist3 is a new copy.

#浅拷贝概念
>>> mylist=[1,[1,2]>>> mylist2=list (mylist)>>> mylist2[1, [ 1, 2]]>>> mylist2[1][1]=5>>> mylist2[1, [1, 5]]>>> mylist[ 1, [1, 5]]

>>> mylist2[1]=[1,2,3]
>>> MyList
[1, [1, 5]]
>>> Mylist2
[1, [1, 2, 3]]

Here there is a shallow copy involved, because Mylist2[1] is just a shallow copying, only copy the reference does not copy the value

But if you re-assign the value to Mylist2[1, then there's no problem.

Python Common errors

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.