Python Basic Data Type--set

Source: Internet
Author: User

I. Definition of a collection

Set set, which is an unordered and non-repeating collection of Elements.

A collection object is a set of unordered, hashed values that a collection member can make as a key in a dictionary. The collection supports checking members with the in and not in operators, which are used by Len () to get the cardinality (size) of the collection and iterate over the members of the collection with a for loop. however, because the collection itself is unordered, you cannot create an index or perform a slice (slice) operation on the collection, nor do you have a key (keys) available to get the values of the elements in the Collection.

Ii. creation of a set
s = Set () s = {11,22,33,44}* note: When you create an empty collection, only set () is used, and if you use the second method s={}, you are actually creating an empty dictionary. s = {}print (type (s)) <class ' dict ' >a=set (' boy ') b=set ([' y ', ' b ', ' o ', ' o ']) c=set ({"k1": ' v1 ', ' K2 ': ' v2 '}) d={' K1 ', ' K2 ', ' K2 '}e={(' K1 ', ' K2 ', ' K2 ')}print (a,type (a)) print (b,type (b)) print (c,type (c)) print (d,type (D)) print (e,type (e)) Execution results are as follows: {' o ', ' b ', ' y '} <class ' set ' >{' o ', ' b ', ' y '} <class ' set ' >{' K1 ', ' K2 '} <class ' set ' >{' K1 ', ' K2 '} <class ' Set ' >{(' K1 ', ' K2 ', ' K2 ')} <class ' Set ' >

third, the collection of functional source code

Basic Features:

    • Add (add, Update)
A=set (' python ') a.add (' tina ') print (a) b=set (' python ') b.update (' Tina ') Print (b) Executes the result as follows: {' Tina ', ' o ', ' P ', ' n ', ' t ', ' y ', ' H '} {' o ', ' i ', ' P ', ' a ', ' n ', ' t ', ' y ', ' H '}################# #由以上代码可以看出, Add is the addition of a single element, and update is a batch Addition. The output is unordered and not added to the Trailer.
    • Delete (REMOVE,DISCARD,POP)
c={' P ', ' i ', ' h ', ' n ', ' o ', ' y ', ' t '}c.remove (' p ') print (c) c={' p ', ' i ', ' h ', ' n ', ' o ', ' y ', ' t '}c.discard (' p ') print (c) c={' p ', ' i ', ' h ', ' n ', ' o ', ' y ', ' t '}c.pop () print (c) Executes the result as follows: {' i ', ' h ', ' t ', ' o ', ' y ', ' n '} # # # # #当执行c. Remove (' P ', ' I ') ' P ', ' i '), error: typeerror:remove () takes exactly one argument (2 given), indicating that remove and discard delete elements can only be deleted, corresponding to the Add. ################################################################################ #remove, The difference between pop and discard: discard deletes the specified element and does not error when the specified element does not exist; remove deletes the specified element, but when the specified element does not exist, error: Keyerror. Pop deletes any element and assigns the removed element to a variable, and cannot specify that the element be Removed.

Unique features of Set:

S1 = {0}S2 = {i% 2 for i in range ()}s = set (' hi ') t = set ([' h ', ' e ', ' l ', ' l ', ' o ']) print (s.intersection (t), S & T) c0/># intersection print (s.union (t), s | t)   # SET Print (s.difference (t), s-t)  # difference Print (s.symmetric_difference (t), s ^ T) # pair The differential set print (s1.issubset (s2), s1 <= s2) # subset (included) print (s1.issuperset (s2), s1 >= s2)   # Parent Set (inclusive) execution result is as follows: {' h '} {' h '} {' i '  , ' e ', ' H ', ' l ', ' o '} {' i ', ' e ', ' H ', ' l ', ' o '} {' i '} {' i '} {' e ', ' l ', ' o ', ' i '} {' e ', ' l ', ' o ', ' i '}true truefalse falses = {11,22,33}t = {22,44}print (s.isdisjoint (t)) # (disjoint Disjointed,) Returns True if there is no intersection, otherwise returns falses.difference_update (t) # Overwrites the difference set to the source collection, that is, remove from the current collection and the same element in B print (s) executes the result as follows: false{33, 11}s = {11,22,33}t = {22,44}s.intersection_update (t) # Overwriting the intersection to the source collection print (s) Executes the result as follows: {22}s = {11,22,33}t = {22,44}s.symmetric_difference_update (t) #将对称差集覆盖到源集合print (s) execution results are as follows: { 33, 11, 44}
Iv. Exercises

Looking for differences: what needs to be deleted? What needs to be built? What needs to be updated?

#!/usr/bin/python#-*-coding:utf-8-*-old_dict = {"#1": {' hostname ': ' C1 ', ' cpu_count ': 2, ' mem_capicity ': 80}, ' # 2 ": {' hostname ': ' C1 ', ' cpu_count ': 2, ' mem_capicity ': +}, ' #3 ': {' hostname ': ' C1 ', ' cpu_count ': 2, ' mem_capicity ': 80  },}new_dict = {"#1": {' hostname ': ' C1 ', ' cpu_count ': 2, ' mem_capicity ': +}, ' #3 ': {' hostname ': ' C1 ', ' Cpu_count ': 2, ' mem_capicity ': "#4": {' hostname ': ' c2 ', ' cpu_count ': 2, ' mem_capicity ': 80},}new_set = set () Old_set = set () fo R i in New_dict:new_set.add (i) to J in old_dict:old_set.add (j) new_add = new_set.difference (old_set) #new_dict中有 , there is no old_del in old_dict = old_set.difference (new_set) #old_dict中有, new_dict does not have update = New_set.intersection (old_set) #old _dict and New_dict together, need to update new_dict to old_dict for k in new_add:old_dict[k] = new_dict[k] #將new_dict中新增的內容添加到old_dict中fo R v in Old_del:del old_dict[v] #將old_dict中失效的內容刪除for m in update:old_dict[m] = new_dict[m] #把new_dict更新到old_ Dict in print (old_dict)

Python Basic Data Type--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.