Python-collection and python collection

Source: Internet
Author: User

Python-collection and python collection

Collection

The main function is to compare the information in the list and test the link.

Features:
1. remove duplicates. If a list is converted into a collection, deduplication is automatically performed.
2. Test the relationship between the two groups of data, such as intersection, difference set, and Union set.
3. No insert function. You can add only.
4. A disordered, non-repeated data combination

5. No insert function. You can only add one.

Appears in the form of {}, type: set

List_1 = [, 33, 1] # create list_1 = set (list_1) # convert the keyword to print (list_1, type (list_1) # output set, and perform de-duplication directly. {1, 2, 3, 4, 5, 33} <class 'set'> # in braces. The data type is set.

Intersection:

List_1 = set ([1, 2, 3, 5, 3, 4, 2, 33, 1]) list_2 = set ([2, 2, 4, 5, 6, 7, 8]) print (list_1.intersection (list_2) # intersection, the duplicate items in the two sets are displayed. {2, 4, 5}

Abbreviation of intersection:

List_1 = set ([1, 2, 3, 5, 3, 4, 2, 33, 1]) list_2 = set ([2, 2, 4, 5, 6, 7, 8]) print (list_1 & list_2) # abbreviation of intersection {2, 4, 5}

Union:

List_1 = set ([, 1]) list_2 = set ([,]) print (list_1.union (list_2) # union, that is, remove and merge the duplicate options in the two lists. {1, 2, 3, 4, 5, 33, 6, 7, 8}

Abbreviation of union.

List_1 = set ([1, 2, 3, 5, 3, 4, 2, 33, 1]) list_2 = set ([2, 2, 4, 5, 6, 7, 8]) print (list_1 | list_2) # Union abbreviations: {1, 2, 3, 4, 5, 33, 6, 7, 8}

The difference set is the data in 1 but not in 2.

List_1 = set ([1, 2, 3, 5, 3, 4, 2, 33, 1]) list_2 = set ([2, 2, 4, 5, 6, 7, 8]) print (list_1.difference (list_2 )) # difference set {1, 3, 33}

Abbreviation of difference set:

List_1 = set ([1, 2, 3, 5, 3, 4, 2, 33, 1]) list_2 = set ([2, 2, 4, 5, 6, 7, 8]) print (list_1-list_2) # difference set abbreviations: {1, 3, 33}

A subset is used to determine whether List 1 contains all the data in list 3.

List_1 = set ([1, 2, 3, 5, 3, 3, 2, 33, 1]) list_3 = set ([1, 2, 3]) print (list_3.issubset (list_1 )) # subset True # Return false or True

The parent set is used to determine whether all content in list 3 is included in List 1.

List_1 = set ([1, 2, 3, 5, 3, 3, 2, 33, 1]) list_3 = set ([1, 2, 3]) print (list_1.issuperset (list_3 )) # True for parent set # false or True

The symmetric difference set removes the numbers in both lists and merges them together.

List_1 = set ([, 1]) list_2 = set ([,]) print (list_1.1_ric_difference (list_2 )) # print (list_1 ^ list_2) # abbreviation of Symmetric Difference set {33, 4, 5, 88}

The intersection is used to determine whether the two lists have the same information.

List_1 = set ([, 1]) list_2 = set ([,]) print (list_1.isdisjoint (list_2 )) false # if there is an intersection, False is displayed. If not, True is displayed.

Add:

List_1 = set ([999, 444,333,222, 33, 1]) list_1.add () # Add print (list_1) list_1.update ([]) # add multiple print (list_1) {1, 2, 3, 4, 5, 33,999} {1, 2, 3, 4, 5, 33,999,333,444,222}

Delete:

List_1 = set ([2,333, 333, 1]) list_1.remove () # Delete print (list_1)

Delete one randomly:

List_1 = set ([1, 2, 3, 5, 4,333]) print (list_1.pop () 1 # Return the deleted Value

Delete. If it is not in the Set, no error is returned.

List_1 = set ([4,333, 222,]) list_1.discard () # No or error in the set.

Show the length after deduplication:

list_1 = set([1,2,3,5,4,333])print(len(list_1))6

Determines whether the number is in the collection.

List_1 = set ([4,333,]) print (6 in list_1) False # If not, False is displayed. If yes, True is displayed.

Calculate whether the number is not in this set.

List_1 = set ([4,333,]) print (6 not in list_1) True # True is displayed if not present, False is displayed if

Copy:

List_1 = set ([1, 2, 3, 5, 4,333]) list_2 = list_1.copy () # copy to list_2. Print (list_2) print (type (list_2) {1, 2, 3, 4, 5,333} <class 'set'>

 

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.