Python path-collection and depth copy

Source: Internet
Author: User

Supplemental: ID Query memory address

Print (ID (name))

is to determine the memory address int str small Data pool

int:-5-256

STR: If the string is made up of letters, it points to a memory address.

If the number is multiplied by STR (a single letter), the same memory address is within 20 (including 20).

Other data types do not have a small data pool concept.

English: str conversion bytes Str.encode (' Utf-8 ')

One, set.

The ① collection is an unordered, non-repeating collection of data.

② The elements inside it are hashed (immutable types), but the collection itself is non-hash int,str,bool () (so the collection does not have a dictionary key).

③ Collection cannot change the elements inside

④ sets can be used to find the intersection, the set, the difference set, the inverse intersection and so on.

Go weight:

1.1 Use algorithm to do.

1.2 Convert to a set, then convert it over.

When you turn a list into a collection, it automatically goes heavy.

1, creation of the collection.
Set1 = Set ({,'Barry' = {,'Barry'} Print (Set1,set2)  # {1, 2, ' Barry '} {1, 2, ' Barry '}
2, the increase of the collection.
Set1 = {'Alex','Wusir','Ritian','Egon','Barry'}set1.add ('King Goddess')Print(Set1)#Update: Iterate incrementsSet1.update ('A')Print(Set1) set1.update ('Teacher')Print(Set1) set1.update ([The])Print(Set1)
3, delete the collection.
Et1 = {'Alex','Wusir','Ritian','Egon','Barry'}set1.remove ('Alex')#Delete an elementPrint(Set1) set1.pop ()#random deletion of an elementPrint(Set1) set1.clear ()#emptying the collectionPrint(Set1)delSet1#Delete CollectionPrint(Set1)
4, the collection of check.

For ... in set

Print (..)

5, the other operations of the collection 5.1 intersect. (& or intersection)
Set1 = {1,2,3,4,5= {4,5,6,7,8}print(Set1 & Set2)  #  {4, 5}  Print(set1.intersection (Set2))  #  {4, 5}
5.2 and set. (| or union)
Set1 = {1,2,3,4,5= {4,5,6,7,8}print(Set1 | set2)  #  {1, 2, 3, 4, 5, 6, 7}print(set2.union (Set1))  #  {1, 2, 3, 4, 5, 6, 7}
5.3 Difference set. (-or diference)
Set1 = {1,2,3,4,5= {4,5,6,7,8}print(Set1-set2)  #  {1, 2, 3}Print (Set1.difference (Set2))  #  {1, 2, 3}
5.4 Inverse intersection. (^ or symmetric_difference)
Set1 = {1,2,3,4,5= {4,5,6,7,8}print(set1 ^ set2)  #  {1, 2, 3, 6, 7, 8}print(set1.symmetric_difference (Set2))  #  {1, 2, 3, 6, 7, 8}
5.5 Subsets and hyper-sets
Set1 = {= = {1,2,3,4,5,6}print(Set1 < set2)print(Set1.issubset ( Set2)  #  These two are the same, both of which illustrate that Set1 is a subset of Set2.  Print(Set2 > set1)print(Set2.issuperset (Set1))  #  These two are the same, It is the description Set2 is Set1 superset. 
6. Frozenset immutable collection to make the collection immutable.
s = Frozenset ('Barry')print(S,type (s))  #  frozenset ( {' A ', ' Y ', ' B ', ' R '}) <class ' Frozenset ' >

2, the depth copy1, first look at the assignment operation.
L1 = [1,2,3,['Barry','Alex']]l2=L1l1[0]= 111Print(L1)#[111, 2, 3, [' Barry ', ' Alex ']Print(L2)#[111, 2, 3, [' Barry ', ' Alex ']l1[3][0] ='Wusir'Print(L1)#[111, 2, 3, [' Wusir ', ' Alex ']Print(L2)#[111, 2, 3, [' Wusir ', ' Alex ']

For an assignment operation, L1 and L2 point to the same memory address, so they are exactly the same.

2, shallow copy (copy)
L1 = [1,2,3,['Barry','Alex']]l2=l1.copy ()Print(L1,id (L1))#[1, 2, 3, [' Barry ', ' Alex ']] 2380296895816Print(L2,id (L2))#[1, 2, 3, [' Barry ', ' Alex ']] 2380296895048L1[1] = 222Print(L1,id (L1))#[1, 222, 3, [' Barry ', ' Alex ']] 2593038941128Print(L2,id (L2))#[1, 2, 3, [' Barry ', ' Alex ']] 2593038941896l1[3][0] ='Wusir'Print(L1,id (l1[3]))#[1, 2, 3, [' Wusir ', ' Alex ']] 1732315659016Print(L2,id (l2[3]))#[1, 2, 3, [' Wusir ', ' Alex ']] 1732315659016

For shallow copy, the first talented woman is a separate memory address, starting from the second layer, all point to the same memory address, a change is changed.

3, deep copy
ImportCOPYL1= [1,2,3,['Barry','Alex']]l2=copy.deepcopy (L1)Print(L1,id (L1))#[1, 2, 3, [' Barry ', ' Alex ']] 2915377167816Print(L2,id (L2))#[1, 2, 3, [' Barry ', ' Alex ']] 2915377167048l1[1] = 222Print(L1,id (L1))#[1, 222, 3, [' Barry ', ' Alex ']] 2915377167816Print(L2,id (L2))#[1, 2, 3, [' Barry ', ' Alex ']] 2915377167048l1[3][0] ='Wusir'Print(L1,id (l1[3]))#[1, 222, 3, [' Wusir ', ' Alex ']] 2915377167240Print(L2,id (l2[3]))#[1, 2, 3, [' Barry ', ' Alex ']] 2915377167304

For deep copy, no matter how many layers, there are two separate memory addresses in memory.

Python path-collection and 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.