Summary of Python_day_04 Set method

Source: Internet
Author: User

Set (SET)

    • Create an empty collection directly
Set_empty = set ()print#  set ()
    • Create based on parameters
# according to the parameter set_argument = set ("345",'ry ' Print#  This will cause an error, because set only allows one parameter
    • Based on the list to create
Set_list = Set ([11,11,'a ','one','ee ' ]print#  {One, ' ee ', ' 45 ', ' 11 '} will automatically delete duplicate objects
    • Based on tuples to create
Set_tuple = Set ((11,11,'One ','one','ee  ',))print#  {' ee ', 11, ' 45 ', ' 11 '} also automatically deletes duplicate objects
    • Create according to a dictionary
Set_dict = Set ({'K1':'v1','K2': 2,'K3':'v3','K1':'V4'})Print(set_dict)#{' K3 ', ' K1 ', ' K2 '} will only store the key value and will not repeat
    • String creation
Set_str = set ('Hello Python')print#  {' y ', ' o ', ' n ', ', ' P ', ' t ', ' h ', ' h ', ' e ', ' l '} will also remove duplicate characters from the string
    • Add (self, *args, **kwargs) adds a new element to the collection, and there is no action if the added element already exists in the collection
Set_init = Set ([11, 11,' $',' One','ee']) Set_init.add ('Hello')Print(Set_init)#{' Hello ', one, ' ee ', ' one ', ' I '}Set_init_str= Set ('Python') Set_init_str.add ('Hello,world')Print(SET_INIT_STR)#{' Hello,world ', ' o ', ' P ', ' t ', ' n ', ' y ', ' h '}
    • Clear (self, *args, **kwargs) clears the elements in the collection and can pass arguments
Set_init_clear = Set ([11,11,'a ','one','ee  ']) set_init_clear.clear ()print#  set ()
    • copy (self, *args, **kwargs) shallow copy, will return a new collection
Set_init_copy = Set ([11,11,'a ','one','ee '  = set_init_copy.copy ()print#  {' EE ', one, ' one ', ' one '}
    • difference (self, *args, **kwargs) compares the two collection classes, and returns a subset of the set that is actively compared to the intersection of the comparison set (mathematically called What is forgotten)
SET_INIT_DIF1 = Set ([11,22,33,44= set ([33,44,55,66= set_init_dif1.difference (SET_INIT_DIF2)  print#  {one, set_init_rece2}= set_init_dif2.difference (SET_INIT_DIF1) Print # {$, +}
    • difference_update (self, *args, **kwargs) Delete two sets of communication
SET_INIT_UPD1 = Set ([11,22,33,44= set ([33,44,55,66]) set_init_upd1.difference_update (SET_INIT_UPD2)  print#  {$, one}= set ([11,22,33,44= set ([33,44,55,66]) set_ Init_upd4.difference_update (set_init_upd3)print#  {in.
    • Discard (self, *args, **kwargs) deletes the element that exists in the collection and does nothing if it does not exist
Set_init_dis = Set ([11,22,33,44,55]) Set_init_dis.discard (one)print#  {33, 44 , 22, 55} can only delete one
    • intersection (self, *args, **kwargs) returns the intersection of two sets
Set_init_inter1 = Set ([11,22,33,44= set ([33,44,55,66= set_init_inter1.intersection (set_init_ INTER2)print#  {in.
    • intersection_update (self, *args, **kwargs) remove the intersection of two sets, update a collection
SET_INTER_UPD1 = Set ([111,222,333,444= set ([333,444,55,66]) set_inter_upd1.intersection_update (set_ INTER_UPD2)print#  {444, 333}
    • Isdisjoint (self, *args, **kwargs) determines whether two sets have intersections, returns false if any, and returns if not ture
SET_ISDIS1 = Set ([111,222,333,444= set ([222,333= set_isdis1.isdisjoint (set_isdis2)  Print#  False indicates an intersection
    • Issubset (self, *args, **kwargs) determines whether a set is a subset of B sets
SET_ISSUB1 = Set ([111,222,333,444= set ([222,333= set_issub1.issubset (set_issub2)  Print#  False indicates that SET_ISSUB1 is not a subset of set_issub2 set_receive_issub2 = Set_issub2.issubset (set_ ISSUB1)print#  True indicates that SET_ISSUB2 is a subset of Set_issub1
    • Issuperset (self, *args, **kwargs) determines whether a collection is the parent of the B collection, or whether the A collection contains a collection of B. Results with issubset (self, *args, **kwargs) Instead
    • pop (self, *args, **kwargs) removes the element and can define a variable to receive the removed element if the set is empty
Set_pop = Set ([111,222,333,444,232= set_pop.pop ()print#  {444, 333, 222, 111}  print#  232
    • Remove (self, *args, **kwargs) Delete element
Set_remove = Set ([111,222,333,444,232]) set_remove.remove (111)print#  {232, 444, 333, 222}
    • symmetric_difference (self, *args, **kwargs) takes out two deviations and updates the same element in a collection if it is deleted at the same time by deleting a A, a, two set.
SET_SYM1 = Set ([1,2,3,4,5= set ([2,3,4,5,6= set_sym1.symmetric_difference (set_sym2)  Print#  {1, 6}= set ([1,2,3,4,5= set ([6= set_sym3.symmetric_ Difference (SET_SYM4)print#  {1, 2, 3, 4, 5, 6}
    • symmetric_difference_update (self, *args, **kwargs) will have a B in the collection, and an update to the A in the set does not
Set_sym_up1 = Set ([1,2,3,4,5= set ([2,3,4,5,6,]) set_sym_up1.symmetric_difference_update (set_sym_up2)  print#  {1, 6}= set ([1,2,3,4,5= set ([6,]) set_sym_ Up3.symmetric_difference_update (SET_SYM_UP4)print#  {1, 2, 3, 4, 5, 6}
    • Union (Self, *args, **kwargs) returns a set of unions and a set of B
set_un1= set ([11,22,33,44= set ([22,33,44,55= set_un1.union (set_un2)print#  { One, one, one, one, one}
    • Update (self, *args, **kwargs) A and B are in the same set, and return a
Set_un_up1 = Set ([111,222,333,444= set ([222,333,444,555]) set_un_up1.update (set_un_up2)  Print#  {555, 333, 111, 444, 222}

Summary of Python_day_04 Set method

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.