Python Set Collection

Source: Internet
Author: User
Tags set set

#!/usr/bin/env Python3
#set is an unordered and non-repeating collection of elements
#访问速度快
#天生解决重复问题

‘‘‘
#在对象中增加一个元素
#传值是列表或字符串
S1 = set () #新建一个空的对象
S1.add (' Andy ') #在对象中增加一个元素
Print (S1)
S1.add (' Andy ')
Print (S1)
‘‘‘

#set中集合的合并
#difference
#difference_update
‘‘‘
#difference value is a list
Compares two or more set sets and generates a new collection
S2 = Set ([' Andy ', ' rain ', ' Baby ', ' Andy ')
Print (s2)

S3 = S2.difference ([' Andy ', ' Rain ']) #比较两个或多个set集合 and generate a new collection
Print (S3)

#difference_update value is a list
#删除当前set中所包涵的参数集合里的元素
S2 = Set ([' Andy ', ' rain ', ' Baby ', ' Andy ')
Print (s2)
S2.difference_update ([' Andy ', ' Rain '])
Print (s2)
‘‘‘


#set中删除集合中的元素
#pop
#remove
#discard
‘‘‘
#pop take the first element in the set set and assign a value to the new object
S2 = Set ([' Andy ', ' rain ', ' Baby ', ' Coco ')
Print (s2)
result = S2.pop ()
Print (Result)
Print (s2)

#remove value is a string
#删除集合中的某个元素, you must specify the element to delete
S2 = Set ([' Andy ', ' rain ', ' Baby ', ' Coco ')
S2.remove (' Andy ')
Print (s2)

#discard value is a string
#移除元素
S2 = Set ([' Andy ', ' rain ', ' baby '])
S2.discard (' Andy ')
Print (s2)
‘‘‘

#set中交集详解 (the intersection is actually repeating)
#intersection
#intersection_update
‘‘‘
#intersection value is a list
#取两个集合中重复的元素, and assign a value to a new object
S2 = Set ([' Andy ', ' rain ', ' Baby ', ' Coco ')
S3 = S2.intersection ([' Rain ', ' Baby ', ' Coco ')
Print (s2)
Print (S3)

#intersection_update value is a list
# takes a repeating element from two sets and updates the repeating element to the original collection, and if there is no duplicate content, the original collection is empty, and if the value passed in is empty, the original collection does not change
S2 = Set ([' Andy ', ' rain ', ' Baby ', ' Coco ')
S2.intersection_update ([' Andy '])
Print (s2)
‘‘‘


#set中差集详解 (the difference is actually not repeating)
#symmetric_difference
#symmetric_difference_update
‘‘‘
#symmetric_difference value is a list
#取出两个集合中没有重复的字符串, and assign a value to a new object
S1 = set ([' Andy ', ' rain ', ' Baby ', ' Coco ')
S2 = set ([' Rain ', ' Coco '])
S3 = s1.symmetric_difference (S2)
Print (S3)

#symmetric_difference_update value is a list
#取出两个集合中没有重复的字符串, and update the original collection
S1 = set ([' Andy ', ' rain ', ' Baby ', ' Coco ')
S2 = set ([' Rain ', ' Coco '])
S1.symmetric_difference_update (S2)
Print (S1)
‘‘‘



‘‘‘
#isdisjoint value is a list
#判断传入的值是否在集合中, if any, returns false if not, returns true if none
S2 = Set ([' Andy ', ' rain ', ' Baby ', ' Coco ')
S3 = S2.isdisjoint ([' Andy ']) #有则返回False
S4 = S2.isdisjoint ([' Hello ']) #没有则返回True
Print (S3)
Print (S4)
‘‘‘



#数据库中原有
Old_dict = {
"#1": {' hostname ': ' C1 ', ' Cpu_count ': 2, ' mem_capicity ': 80},
"#2": {' hostname ': ' C1 ', ' Cpu_count ': 2, ' mem_capicity ': 80},
"#3": {' hostname ': ' C1 ', ' Cpu_count ': 2, ' mem_capicity ': 80}
}

#cmdb newly reported data
New_dict = {
"#1": {' hostname ': ' C1 ', ' Cpu_count ': 2, ' mem_capicity ': 800},
"#3": {' hostname ': ' C1 ', ' Cpu_count ': 2, ' mem_capicity ': 80},
"#4": {' hostname ': ' C2 ', ' Cpu_count ': 2, ' mem_capicity ': 80}
}


#方法一: Using Set Collection
Old = set (Old_dict.keys ())
New = Set (New_dict.keys ())

Update_set = old.intersection (new)
Delete_set = Old.symmetric_difference (Update_set)
Add_set = New.symmetric_difference (Update_set)

Print (Update_set)
Print (Add_set)
Print (Delete_set)



#方法二: Use for loop and if judgment
Old_set = Set (Old_dict.keys ())
Update_list = List (Old_set.intersection (New_dict.keys ()))

New_list = []
Del_list = []

For I in New_dict.keys ():
If I not in update_list:
New_list.append (i)

For I in Old_dict.keys ():
If I not in update_list:
Del_list.append (i)

Print (update_list,new_list,del_list)

Python 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.