Python Learning---[set set] under Python

Source: Internet
Author: User
Tags set set

Set Set [mutable] is an unordered, non-repeating data combination whose main function is as follows:

1. Go to the weight, turn a list into a set, and then automatically go heavy.

2. Relationship test, test the intersection of two sets of data, difference set, and set of relations

set: A set of different elements that form a collection, which is the basic data type of Python.

Collection Classification : mutable collections, immutable collections

Mutable collection (SET): Elements that can be added and removed, non-hashed [but requiring their own elements to be hash], cannot be used as keys to a dictionary, or elements of other collections

Immutable set (Frozenset): Contrary to the above

Create a combination:

Created with Set ()/Frozenset (), and the element must be immutable, using the curly braces {} output Wrapper

# error Creation S = Set (' A ', 1, ' B ') print (s)          # Typeerror:set expected at most 1 arguments, got 3 Li = [1, 2, ' A ', ' B ']s = set ( LI)       # list changed to set print (s)  # {1, 2, ' A ', ' b '}li2 = [1, 2, 1, ' A ', ' a ']s = set (Li2) print (s)          # go back: {1, 2, ' a '}li = [[ 1, 2,], ' A ', ' B ']s = Set (LI)   # error, typeerror:unhashable type: ' list ' # sets1 = set (' Hello ') print (S1)         # {' H ', ' l ', ' O ', ' e '}# frozensets2 = Frozenset (' hhh ') print (s2)         # Frozenset ({' H '})
Accessing Collections

Because the collection itself is unordered, you cannot create an index or slice operation for the collection, but you can iterate through or use in and not to access or judge the collection element.

S1 = set (' Hello World ') print (' A ' in S1)       # trueprint (' B ' in S1)       # false# s1[1]  #TypeError: ' Set ' object does no T support indexing# Loop iterative unordered output for I in S1:    print (i, end= ' \ t ')   # e W o r d L h# Direct output print (S1)    # {' H ', ' W ', ' R ', ' L ', ' o ', ' d ', ' e ', '} input with spaces, output with spaces
Update Collection

S.add (): element as a whole added in
S.update (): Must be added is a sequence, list [can be understood as added, but is a character of a character serialized add]
S.remove (): Remove an element
S.pop (): Deletes any one element because the set set is unordered, and the randomness of the deletion
S.clear (): Clears the value inside the set
Del set (): Delete Set collection

S1 = set (' Apple ') print ("Original set set  :", S1) s1.add (' Samsung ') print ("Set after Add   :", S1) s1.update (' Huawei ') print (" Update set: ", S1) s1.remove (' a ')           # because a single character after the sequence is added, you cannot enter print directly (" Set after Remove: ", S1) S1.pop () print (" Post-pop set   : ", S1) s1.clear () print (" Clear after set: ", S1) del s1# print (" Set after del   : ", S1)  # error nameerror:name ' s1 ' is not de Fined

Collection type operator

1 in, not in
2 set equivalence and not equivalent (= =,! =)
3 subsets, Hyper-sets

4 Union (|) The operation is actually equivalent to the or operation of the set, and the Union symbol has an equivalent method, Union ()

subsets and Superset: The parent set must be more than a subset and one character

S1 is S0 's parent collection: S1.issuperset (s0) ==> S1 > S0

S0 is a subset of S1: S0.issubset (S1) ==> S0 < S1

S1 = set (' Applesamsung ') S0 = set (' Apple ') print ("Original set set [S0]   :", S0) print ("original set set [S1]   :", s1) print (' S1 is S0 's parent collection    : ', S1 > S0)   # The parent set must be more than a subset and a character print (' S1 is s0 issuperset: ', S1.issuperset (S0))   # The parent set must be more than a subset and a character print (' S0 is the parent collection of S1    : ', S0 < S1)   # The parent set must be more than a subset and a character print (' S0 is S1 issubset  : ', S0.issubset (S1))   # The parent set must be more than a subset and one character

intersection: s0.intersection (S1) ==> S0 & S1

set: s0.union (S1) ==> S0 | S1

difference: s0.difference (S1) ==> s0-s1 "S0-based contrast, in S0 not in S1"

symmetric difference set: s0.symmetric_difference (S1) ==> S0 ^ S1

S0 = Set ([1, 2, 3, 4, 5]) S1 = set ([4, 5, 6, 7, 8]) print ("Original set:s0:", S0) print ("Original set:s1:", s1) print ("Intersection"  and: ", s0.in Tersection (S1)  # take out intersection print ("intersection" S0&S1: ", S0&S1)  # take out intersection print (" "and set"     : ", S0.union (S1))         # Remove and set print ("" and set "S0|S1:", S0 | S1)             # Remove and set print ("S0 difference Set"   : ", S0.difference (S1))    # with S0 as Main, in S0 not in S1print (" "Difference Set" S0-S1: ", s0-s1)    # Mainly S0, in S0 not in S1print ("" "S1 Difference Set"   : ", S1.difference (S0))    # with S1 as Main, in S1 not in S0print (" "Difference Set" S1-s0: ", S1-s0) 
   # is dominated by S0, in S0 No in S1print ("Symmetric difference Set"      : ", S1.symmetric_difference (S0))  # Except for elements outside the common print (" symmetric difference Set "S0 ^s1:" , S1 ^ s0)  # Except for elements outside the public element

Set Set Application: The simplest way to remove weight
"Easiest way to go heavy" lis = [1,2,3,4,1,2,3,4]print list (set (LIS))    #[1, 2, 3, 4]

Python Learning---[set set] under 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.