码迷,mamicode.com
首页 > 其他好文 > 详细

PYD7- 数据类型set、三元运算、函数

时间:2017-01-27 00:17:28      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:move   否则   sel   分享   rabl   hal   没有   x11   play   

1、set

set集合,是一个无序且不重复的元素集合

技术分享
class set(object):
    """
    set() -> new empty set object
    set(iterable) -> new set object
     
    Build an unordered collection of unique elements.
    """
    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
 
    def clear(self, *args, **kwargs): # real signature unknown
        """ Remove all elements from this set. 清除内容"""
        pass
 
    def copy(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of a set. 浅拷贝  """
        pass
 
    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
 
    def difference_update(self, *args, **kwargs): # real signature unknown
        """ Remove all elements of another set from this set.  从当前集合中删除和B中相同的元素"""
        pass
 
    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
 
    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
 
    def intersection_update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the intersection of itself and another.  取交集并更更新到A中 """
        pass
 
    def isdisjoint(self, *args, **kwargs): # real signature unknown
        """ Return True if two sets have a null intersection.  如果没有交集,返回True,否则返回False"""
        pass
 
    def issubset(self, *args, **kwargs): # real signature unknown
        """ Report whether another set contains this set.  是否是子序列"""
        pass
 
    def issuperset(self, *args, **kwargs): # real signature unknown
        """ Report whether this set contains another set. 是否是父序列"""
        pass
 
    def pop(self, *args, **kwargs): # real signature unknown
        """
        Remove and return an arbitrary set element.
        Raises KeyError if the set is empty. 移除元素
        """
        pass
 
    def 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. 移除指定元素,不存在保错
        """
        pass
 
    def 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
 
    def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
        pass
 
    def 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
 
    def update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the union of itself and others. 更新 """
        pass
常用方法
技术分享
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# se = {11,22,33,44}
# se.add(55)
# print(se)
# se.discard(66)
# #se.remove(66)
# print(se)
# bf = {21,22,23,25}
#
# #取se bf的交集
# ret1 = se.intersection(bf)
# #取交集并更新se
# se.intersection_update(bf)
#
# print(ret1)
# print(se)
#
# ret2 = se.issubset(bf)
# ret3 = se.issuperset(bf)
# print(ret2)
# print(ret3)
#
# bf.pop()
# print(bf)

se = {11,22,33,44}
be = {11,22,77,55}
r1 = se.difference(be)
r2 = be.difference(se)
print(r1)
print(r2)
ret = se.symmetric_difference(be)
print(ret)
# se.symmetric_difference_update(be)
# print(se)
ret = se.union(be)
print(ret)
print(se)
se.update([21])
print(se)
示例代码1

1.1习题:

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 }
}
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 }
}
#老字典key 相同的键值,将新字典key值更新到old,
#老字典中存在,新字典不存在的 将old中的值删除
目的:更新数据源
技术分享
#!/usr/bin/env python
# -*- coding:utf-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 }
}
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 }
}
old_keys = old_dict.keys()
new_keys = new_dict.keys()
old_set = set(old_keys)
new_set = set (new_keys)
del_set = old_set.difference(new_set)
add_set = new_set.difference(old_set)
update_set = old_set.intersection(new_set)
部分代码

 

 

PYD7- 数据类型set、三元运算、函数

标签:move   否则   sel   分享   rabl   hal   没有   x11   play   

原文地址:http://www.cnblogs.com/workherd/p/6352247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!