Python Collection Set

Source: Internet
Author: User
Tags new set shallow copy

# Set auto-weight, but no value, only key
s = Set ([3, 5, 9, ten]) # Create a collection of values

t = Set ("Hello") # Creates a collection of unique characters

x = ' x '

A = T | the set of S # T and S

b = Intersection of T & S # T and S

c = T -S # differential set (items in T, but not in s)

D = t ^ s # symmetric difference set (items in t or S, but not both)

# Basic Operation:

T.add (' x ') # Add an item

S.update ([Ten, Panax Notoginseng]) # Add multiple items in S

# Use Remove () to delete an item:

T.remove (' H ')

Len (s)
# The length of the set

X in S
# Test if X is a member of S

X not in S
# Test if X is not a member of S

S.issubset (t)
S <= t
# Test if every element in S is in t

S.issuperset (t)
S >= t
# Test if every element in T is in S

S.union (t)
s | T
# Returns a new set containing each element in S and T

S.intersection (t)
S & T
# Returns a new set containing the common elements in S and T

S.difference (t)
S-t
# Returns a new set containing elements in s but not in t

S.symmetric_difference (t)
s ^ t
# Returns a new set containing elements that are not duplicates in S and T

S.copy ()
# Returns a shallow copy of set "s"

Python Collection Set

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.