Python Basics-Day 2 Learning Note-set Collection

Source: Internet
Author: User
Tags exit in

A collection is an unordered, non-repeating combination of data, and its main functions are as follows:

    • Go to the weight, turn a list into a set, and then automatically go heavy.
    • relationship Test , test the intersection of two sets of data, difference set, and the relationship between the set
    • Set is divided into mutable sets and immutable sets (Frozon set).
      • Mutable collections can be added or removed, but Frozon set cannot be
      • Mutable sets are not hashed, so you cannot do dictionary keys. However, the Frozon set has a hash value and can be used as the key value.
Create

Set and list ([]) and dictionary ({}) are different, there is no syntax format. Only created with the factory method set () or Frozenset ().

list_1= [1,4,8,9,4,10,12= Set (list_1)print(List_1,type (list_1))
View Code

{1, 4, 8, 9, 10, 12} <class ' Set ' >

Add to
List_3.add (' C ')
Print (List_3)
List_4.update ([888,777])
Print (List_4)

{1, 3, ' C ', 7}
{2, 4, 6, 777, 888}

Delete
List_3.remove (3)  the difference between a #remove and a discard is whether it is an error. Remove, discard friendly-do nothing if the element does not exit in the Set.print (list_3)

{1, ' C ', 7}

*pop () Random deletion

Relationship Testing
List_1 = Set ([1,4,8,9,4,10,12= set ([2,3,4,8,11,12])print(list_1.intersection (list_2))    # intersection, available list_1 & List_2 instead

{8, 4, 12}

Print (List_1.union (list_2))    # set and de-heavy, usable list_1 | list_2 instead

{1, 2, 3, 4, 8, 9, 10, 11, 12}

Print (List_1.difference (list_2))    # difference set =in list_1 but not in list_2; usable list_1-list_2

{1, 10, 9}

Print (List_1.issubset (list_2))   #  Print(List_1.issuperset (list_2))  # Determines if the parent set

False

Print (List_1.symmetric_difference (list_2))  # Symmetric difference set: The set-intersection section; available list_1 ^ list_2

{1, 2, 3, 9, 10, 11}

List_3 = Set ([1,3,7= set ([2,4,6])print(List_3.isdisjoint (list_4))    #是否完全无关?

True

Member relationship (in, not in) & equivalence/not equivalent

As with other container types, the set still supports in or not in, equivalence equivalent of the operation symbols, and Len () to get the set cardinality (size), with a for loop iteration of the collection of members. However, you cannot create an index or slice.

Python Basics-Day 2 Learning Note-set 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.