Python Base---Collection

Source: Internet
Author: User

Set: A mutable data type in which the elements inside must be immutable data types, unordered, and not duplicated. {}
1Set1 = Set ({})2 Print(Set1)3 4 #Add Add,update5Set1 = {'Alex','Wusir','Ritian','Egon','Barry'}6Set1.add ('Goddess')7 Print(Set1)#unordered8 #{' goddess ', ' Egon ', ' Barry ', ' Wusir ', ' Alex ', ' Ritian '}9 TenSet1.update ('Male Gods')#The iteration is incremented, which is equivalent to the extend in the list, by the object one by one plus One Print(Set1) A #{' Male ', ' goddess ', ' Egon ', ' Barry ', ' Wusir ', ' Alex ', ' God ', ' Ritian '} -  - #Delete Pop,remove,del, clear theSet1.pop ()#random Delete, with return value - Print(Set1) -  -Set1.remove ('Elements')#Delete by Element + Print(Set1) -  +Set1.clear ()#emptying the collection A Print(Set1) at  - delSet1#Delete Collection - Print(Set1) -  - #Check -  forIinchSet1: in     Print(i)

Intersection &

The collection |

Difference Set-

Inverse intersection ^

  
1 set1 = {1,2,3 2 Set2 = {1,2,3,4,5,6} 3 4 Print (set1 < Set2) 5 print (Set1.issubset (Set2)) # These two are the same, both of which illustrate that Set1 is a subset of Set2. 6 7 Print (set2 > Set1) 8 print (Set2.issuperset (Set1)) # These two are the same, both of which illustrate that Set2 is a set1 superset.

The set is mutable, the element is immutable, and the Frozenset becomes a set invariant

1 s = frozenset ('Barry')2print(S,type (s) ) 3  for inch S: 4     Print (i)

Depth copy

1 #赋值运算 2 l1 = [3] L2 = L14 l1.append ('a'  )5print("{}\t\t{}". Format (L1,L2))  6#[1, 2, 3, ' a ']        [1, 2, 3, ' a ']
1 #浅copy2L1 = [A]3L2 =l1.copy ()4 Print(L1,L2)5 Print(ID (L1), id (L2))6L2.append ('a')7 Print(L1,L2)8 """9 [1, 2, 3] [1, 2, 3]Ten 1848031189768 1848031187272 One [1, 2, 3] [1, 2, 3, ' a '] A """
1L1 = [1,2,[4,5,6],3]2L2 =l1.copy ()3 Print(L1,id (L1))4 Print(L2,id (L2))5L1.append ('a')6 Print(L1,L2)7L1[2].append ('a')8 Print(L1,L2)9 Print(ID (l1[2]))Ten Print(ID (l2[2])) One """ A [1, 2, [4, 5, 6], 3] 2318104334664 - [1, 2, [4, 5, 6], 3] 2318103951432 - [1, 2, [4, 5, 6], 3, ' a '] [1, 2, [4, 5, 6], 3] the [1, 2, [4, 5, 6, ' a '], 3, ' a '] [1, 2, [4, 5, 6, ' a '], 3] - 2318104337160 - 2318104337160 -  + """

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.

Deep copy

1 ImportCopy2L1 = [1,2,[4,5,6],3]3L2 =copy.deepcopy (L1)4 Print(L1,id (L1))5 Print(L2,id (L2))6L1[2].append ('a')7 Print(L1,L2)8 9L1 = [1,[1],2,3,4]TenL2 =l1[:] OneL1[1].append ('a') A  - Print(L1,id (L1)) - Print(L2,id (L2))

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.

Python Base---Collection

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.