首页 > 编程语言 > 详细

python---依葫芦画瓢之set集合

时间:2017-03-08 22:17:43      阅读:156      评论:0      收藏:0      [点我收藏+]
#! /usr/bin/env python
#coding=utf-8
#三元运算
‘‘‘
result = 8 if 5 < 4 else 9
print result # 9

result = 8 if 5 > 4 else 9
print result # 8
‘‘‘
# 数据库中原有
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 新汇报的数据
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},
}
s1 = set(old_dict)
s2 = set(new_dict)
#print s1
#print "_______________________________"
#print s2
‘‘‘set([‘#3‘, ‘#2‘, ‘#1‘])
_______________________________
set([‘#3‘, ‘#1‘, ‘#4‘])‘‘‘

def add(self, *args, **kwargs): # real signature unknown
"""
Add an element to a set,添加元素

This has no effect if the element is already present.
"""
pass
‘‘‘
s1.add(‘#5‘)
print s1

#set([‘#3‘, ‘#2‘, ‘#1‘, ‘#5‘])‘‘‘

def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. 清除内容"""
pass
‘‘‘s1.clear()
print s1
#set([])
‘‘‘
def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a set. 浅拷贝 """
pass
‘‘‘
print s1.copy()
#set([‘#3‘, ‘#2‘, ‘#1‘])
‘‘‘
def difference(self, *args, **kwargs): # real signature unknown
"""
Return the difference of two or more sets as a new set. A中存在,B中不存在

(i.e. all elements that are in this set but not the others.)
"""
pass
‘‘‘
s1 = set([‘#3‘, ‘#2‘, ‘#1‘])
s2 = set([‘#3‘, ‘#1‘, ‘#4‘])
print s1.difference(s2)
#set([‘#2‘])
‘‘‘
def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. 从当前集合中删除和B中相同的元素"""
pass
‘‘‘
s1 = set([‘#3‘, ‘#2‘, ‘#1‘])
s2 = set([‘#3‘, ‘#1‘, ‘#4‘])
s1.difference_update(s2)
print s1
#set([‘#2‘])
‘‘‘
def discard(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set if it is a member.

If the element is not a member, do nothing. 移除指定元素,不存在不保错
"""
pass
‘‘‘
s1.discard(‘#2‘)
print s1
#set([‘#3‘, ‘#1‘])
‘‘‘
def intersection(self, *args, **kwargs): # real signature unknown
"""
Return the intersection of two sets as a new set. 交集
(i.e. all elements that are in both sets.)
"""

pass
‘‘‘
s3 = s1.intersection(s2)
print s3
#set([‘#3‘, ‘#1‘])
‘‘‘
def intersection_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the intersection of itself and another. 取交集并更更新到A中 """
pass
‘‘‘
s1.intersection_update(s2)
print s1
#set([‘#3‘, ‘#1‘])
‘‘‘
def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False"""
pass
‘‘‘
print s1.isdisjoint(s2)
#False
‘‘‘

def issubset(self, *args, **kwargs): # real signature unknown """ Report whether another set contains this set. 是否是子序列""" pass‘‘‘print s1.issubset(s2)#False‘‘‘def issuperset(self, *args, **kwargs): # real signature unknown """ Report whether this set contains another set. 是否是父序列""" pass‘‘‘print s1.issuperset(s2)#False‘‘‘def pop(self, *args, **kwargs): # real signature unknown """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. 移除元素 """ passdef remove(self, *args, **kwargs): # real signature unknown """ Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError. 移除指定元素,不存在保错 """ passdef symmetric_difference(self, *args, **kwargs): # real signature unknown """ Return the symmetric difference of two sets as a new set. 对称差集 (i.e. all elements that are in exactly one of the sets.) """ pass‘‘‘print s1.symmetric_difference(s2)#set([‘#2‘, ‘#4‘])‘‘‘def symmetric_difference_update(self, *args, **kwargs): # real signature unknown """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """ passdef union(self, *args, **kwargs): # real signature unknown """ Return the union of sets as a new set. 并集 (i.e. all elements that are in either set.) """ pass‘‘‘print s1.union(s2)#set([‘#3‘, ‘#2‘, ‘#1‘, ‘#4‘])‘‘‘def update(self, *args, **kwargs): # real signature unknown """ Update a set with the union of itself and others. 更新 """ pass‘‘‘s1.update(s2)print s1#set([‘#3‘, ‘#2‘, ‘#1‘, ‘#4‘])‘‘‘print "删除" + str (s1.difference(s2))print "新建" + str (s2.difference(s1))print "更新" + str (s1.intersection(s2))

python---依葫芦画瓢之set集合

原文:http://www.cnblogs.com/longqihui/p/6523190.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!