Python Collection Depth Copy

Source: Internet
Author: User

01 Yesterday's Content review

Code block: One file, one function, one module, one class, and each line in interactive mode is a block of code

is = = Id:id () is the query memory address, = = Compare the values on both sides, is to compare the memory address on both sides.

Small Data pools:

Create a pool in memory, pre- -5~256 integer, a certain regular string, subsequent programs, if the set of variables point to the small data content, it will not be re-created in memory.

1. Save Memory

2. Improve performance and efficiency.

The relationship between small data pools and code blocks.

The same block of code: When Python executes, it encounters an initialization object, and he places the variable and value in a dictionary, which he will look for in the dictionary again.

Different code blocks: Python executes, looking directly from the small data pool. Satisfies the same condition ID.

Coding:

Str "Bytes:encode

Bytes "Str:decode

02 Homework Explanation

03 Basic Data Type Supplement

Ganso: If there is only one data in the progenitor, and there is no comma, then the ' progenitor ' is the same data type as the data inside. Print () will get the type that belongs to the element, not the progenitor.

tu1= (1)

Print (TU1) Gets the element

tu1= (1,)

Tu2= (' Alex ',)

Tu3= ("the")

lists, tuples, strings convert each other

Lists and tuples converted to strings must depend on the Join function

Lists: Lists and lists can be added

L1= "the"

L2= "' Alex ', ' Wusir '"

L3=l1+l3

Print ()

When you loop a list, if you delete some of the elements in the list,

Then all elements behind the element will advance one, they will change the index position, so can not be in the loop, the time to make additions and deletions

Eg:# for I in range (Len (LI) -1,-1,-1):
# If I%2==1:
# Li.pop (i)
# Print (LI)

Dictionary

Dic1=dic.fromkeys ([+], ' Alex ')

Print (DIC1)

When you loop a dictionary, you cannot change the size of the dictionary (that is, you cannot add or subtract).

The conversion of the data type.

INT STR BOOL Conversion

Str "Types"

Str "list: When a string is converted to a list with split, and the list is converted to a string, the list must be full of strings."

04 Collection Set

Added: there is Add. Update#update: Iterative Increase
Set1.update (' A ') print (SET1)
Set1.update (' teacher ') print (SET1)
Set1.update ([+]) print (SET1)

By deleting:

Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}
Set1.remove (' Alex ') # Delete an element print (SET1)
Set1.pop () # Random deletion of an element print (SET1)
Set1.clear () # Empty set print (Set1)
Del Set1 # Delete collection print (Set1)

Set:

{' name ', ' Alex ', ' a '

A collection requires that the element inside must be an immutable data type, but itself can be changed, and the elements inside the collection are not duplicated and unordered.

Main uses:

    1. Go heavy,
    2. Relationship testing.

List to go heavy

li=[1,2,2,3,4,6,1,3,]
# Set1=set (LI)
# li=list (Set1)
# # print (SET1)
# Print (LI)
Collections like #
Set1=set ({A-i})
# Set2=set1
# Print (ID (SET1), ID (Set2))
The address is not the same.
    1. Relationship Testing

Intersection &intersection

and set |union

Difference Set-difference

Inverse intersection ^ or symmetric_difference

Subsets and hyper-sets

05 Light Copy

For shallow copy, the first layer creates a new memory address, and from the second level it points to the same memory address, so consistency is maintained for the second layer and the deeper layers.

L1 = [1,2,3,[' Barry ', ' Alex ']

L2 = L1.copy ()

Print (L1,id (L1)) # [1, 2, 3, [' Barry ', ' Alex ']] 2380296895816

Print (L2,id (L2)) # [1, 2, 3, [' Barry ', ' Alex ']] 2380296895048

L1[1] = 222print (L1,id (L1)) # [1, 222, 3, [' Barry ', ' Alex ']] 2593038941128print (L2,id (L2)) # [1, 2, 3, [' Barry ', ' Alex '] ] 2593038941896

L1[3][0] = ' Wusir '

Print (L1,id (l1[3)) # [1, 2, 3, [' Wusir ', ' Alex ']] 1732315659016

Print (L2,id (l2[3)) # [1, 2, 3, [' Wusir ', ' Alex ']] 1732315659016

05 Depth Copy

For deep copy, the two are completely independent, changing any element of any one (no matter how many layers), and the other absolutely does not change

Import Copy

L1 = [1,2,3,[' Barry ', ' Alex ']

L2 = copy.deepcopy (L1)

Print (L1,id (L1)) # [1, 2, 3, [' Barry ', ' Alex ']] 2915377167816

Print (L2,id (L2)) # [1, 2, 3, [' Barry ', ' Alex ']] 2915377167048

L1[1] = 222

Print (L1,id (L1)) # [1, 222, 3, [' Barry ', ' Alex ']] 2915377167816

Print (L2,id (L2)) # [1, 2, 3, [' Barry ', ' Alex ']] 2915377167048

L1[3][0] = ' Wusir '

Print (L1,id (l1[3)) # [1, 222, 3, [' Wusir ', ' Alex ']] 2915377167240

Print (L2,id (l2[3)) # [1, 2, 3, [' Barry ', ' Alex ']] 2915377167304

Python Collection Depth Copy

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.