首页 > 技术文章 > 集合和函数

ithome0222 2018-03-28 22:22 原文

集合(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
python源代码

操作方法:

1.定义集合

s={1,2,3,4,5,6}   #该种方法操作的元素为内容不可改变对象,如int,char,str

s=set(['alex','alex','sb'])  # 这种方法元素无限制

2.往集合中加入元素

s.add('s')

3.清空集合中的元素,仅留下空集合

s.clear()

4. 删除集合中的元素

#随机删s.pop()

#指定删除s.remove('sb')s.remove('hellol') #删除元素不存在会报错s.discard('sbbbb')#删除元素不存在不会报错

 

集合的特殊操作

1. 求交集

print(p_s.intersection(l_s))
print(p_s&l_s) 集合中的元素没有改变,只是输出的结果为交集

2. 求并集

print(p_s.union(l_s))
print(p_s|l_s)

3. 差集

print('差集',p_s-l_s)
print(p_s.difference(l_s))

4.交叉补集

print('交叉补集',p_s.symmetric_difference(l_s))
print('交叉补集',p_s^l_s)

5. 判断两个集合是否不相交

print(s1.isdisjoint(s2)) #不相交返回结果False,否则为True

6.判断是否是子集或者父集

print(s1.issubset(s2))#s1 是s2 的子集
print(s2.issuperset(s1))#s1 是s2 的父集

7. 更新

s1.update(s2) #更新多个值

8. 求出差集,并把结果覆盖调用该方法的集合

p_s.difference_update(l_s)

 

二· 函数

函数的作用:

1. 提高代码复用性

2.便于后期开发和维护

3. 使逻辑更加清晰

函数的定义

def 函数名(参数):
       
    ...
    函数体
    ...
    返回值

函数的定义主要有如下要点:

  • def:表示函数的关键字
  • 函数名:函数的名称,日后根据函数名调用函数
  • 函数体:函数中进行一系列的逻辑计算,如:发送邮件、计算出 [11,22,38,888,2]中的最大数等...
  • 参数:为函数体提供数据
  • 返回值:当函数执行完毕后,可以给调用者返回数据。

函数定义的例子:

def test01():
    msg = 'test01'
    print(msg)

  

推荐阅读