Elaborate on the collection in Python

Source: Internet
Author: User

    • Function of the collection:

1) go to the weight: a list into a set, it automatically go heavy;

2) Relationship test: The intersection, difference set and set of two sets of data are obtained;

1 #go to heavy instance2list=[1,34,2,45,34,67,89,67]3list=set (list)4 Print(list)5 Print(Type (list))6 7 #result8 #changes to the data have been heavy, and the list has become set; Note: collections are unordered.9{1, 34, 67, 2, 45, 89}Ten<class 'Set'>

    • Collection of additions and deletions to change:
1List=set ([1,34,2,45,34])2 #add: Only one data can be added3List.add (10)4 Print(list)5 #Update: Multiple data can be added6List.update ([11,77,88])7 Print(list)8 #Delete Remove,remove error when deleting non-existent data9List.remove (45)Ten Print(list) One #deleting Pop:pop is the random deletion of data in set A Print(List.pop ()) - #Delete Discard,discard does not error when deleting non-existent data; -List.discard (40) the Print(list) -  - #result -{10, 1, 34, 2, 45} +{1, 34, 2, 10, 11, 45, 77, 88} -{1, 34, 2, 10, 11, 77, 88} +1 A{34, 2, 10, 11, 77, 88}
1list=[2,44,56]2list1=[34,2,44,56,78,89,90]3list=set (list)4list1=Set (List1)5 6 #subset7A=List.issubset (List1)8 Print(a)9 #Parent SetTenb=list1.issuperset (list) One Print(b) A  -  - #result the True -True
1list=[1,34,2,45,34,67,89,67]2list1=[34,2,44,56,78,89,90]3 4list=set (list)5list1=Set (List1)6 7 #intersection (&)8A=list.intersection (List1)9 Print(a)Ten #The Set (|) Oneb=list.union (List1) A Print(b) - #difference Set (-) -C=list.difference (List1) the Print(c) - #对称difference = set-intersection (^) -D=list.symmetric_difference (List1) - Print(d) +  - #result +{89, 34, 2} A{1, 34, 67, 2, 44, 45, 78, 56, 89, 90} at{1, 67, 45} -{1, 67, 56, 90, 44, 45, 78}
  1  list=[1,34,2,45,34]   2  list1=[3,44,56,78,89,90"   3   4  list= Set (list)   5  list1=set (list1)   6  #   If there are no intersections between the two sets of data, Returns True, otherwise false   7  f=list.isdisjoint ( List1)   8  print   (f)   9  10  #  result  11  True 

Elaborating on collections in Python

Related Article

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.