superfang
及时当勉励,岁月不待人
一 集合 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

set 集合class
集合 class
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'liujianzuo'
a={1:2,4:5}
b=set(a)

b.add(111111)
b.clear()
print(b)

n = {11,22,33}
b = {22,66}
c = {11}

# n中有的b中没有的赋值给新的变量 打印
new_n = n.difference(b)
print(new_n)

#n中有的 b中没有的 更新到n
# n.difference_update(b)
# print(n)

# 将迭代的序列加入
n.update("al")
n.update([1,3,4])
print(n)

#n存在的b不存在的 b存在n不存在的 组合一起输出
ret=n.symmetric_difference(b)
ret2 = n.symmetric_difference({11,22,33})
print(ret)
print("=========")
# n存在的b不存在的 b存在n不存在的 组合一起 更新到前面的集合
n.symmetric_difference_update(b)
print(n)

#  是否是子集  不是返回false  是的话True
ret = n.issubset(c)  # n是不是c的子
print(ret)
ret1 = c.issubset(n)  # c是不是n的子
print(ret1)
ret2 = n.issuperset(c) # n是不是c的父
print(ret2)


# pop discard remove 三个删除的区别

#pop 删除同时可以获取到该值
ret = n.pop() #由于集合是无序的,所以pop删除也是无序的
print(ret)
# discard 删除集合元素,如果元素不存在 返回False 不报错
n.discard(11)
print(n)
#remove 删除集合元素 如果元素不存在 报错
#n.remove(99)

z = {11,22,33}
p = {44}

z.intersection(p)  #取交集并更新到z中
print(z)


集合 练习

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'liujianzuo'

old_dict = {
    "#1":11,
    "#2":11,
    "#3":11,
}
new_dict = {
    "#1":33,
    "#4":22,
    "#7":100,
}
# b = set(old_dict.keys())
# print(b)
s1 =set()
s2 =set()
for i in  old_dict.keys():
    s1.add(i)
for n in new_dict:
    s2.add(n)
print(s1,s2)
ret1 = s1.difference(s2)
print("for deling :",ret1)
ret2 = s2.difference(s1)
print("for adding:",ret2)
# ret=s1.intersection(s2)
# print(ret)
for i in s1 :
    del old_dict[i]

for i in  s2:
    old_dict[i]=new_dict[i]

print(old_dict)

set集合练习
集合练习

交集

>>> a.append(10)
>>> set(a)
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> b =range(6)
>>> a = set(a) 
>>> b =set(b)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> b
set([0, 1, 2, 3, 4, 5])
取交集
>>> a & b
set([0, 1, 2, 3, 4, 5])
交集应用场景:比如两个班级都叫alex或者两个班级都是100分的 或者两个班级中都大于多少分的

并集应用
>>> a | b
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

差集
>>> a - b
set([8, 9, 10, 6, 7])

对称差集

>>> a - b
set([8, 9, 10, 6, 7])
把两个里面对方都没有的打印出来


A b 差集
a ^ b

set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> b
set([0, 1, 2, 3, 4, 5, 12])
>>> a ^ b
set([6, 7, 8, 9, 10, 11, 12])
a.add集合中添加一项,同样的去重不同样的增加
>>> a.add(0) 
>>> a.add(22)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22])

a.update集合添加多项
>>> a.update([23,34,35])
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 34, 35, 22, 23])

添加一个集合到另一个集合
>>> a.update(b)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 34, 35, 22, 23])
#b.issubset(a) a.issuperset(b) 测试b中的每一个元素都在a中
>>> len(a)
>>> len(b)
>>> b in a
False
>>> a in a
False
>>> import tab
>>> b.issubset(a)  #测试b中的每一个元素都在a中
True
>>> a.issubset(b)
False
>>> 3 in a
True
>>> b.issuperset(a)
False
>>> a.issuperset(b)  #测试b中的每个元素都在a中
True

set集合练习
集合练习

三、三元运算符

 

python 的三元运算

  变量名 = 变量1 if 条件判断成立 else 变量2

      意指: 条件成立 变量名值为变量1 否则为变量2

 

name = "eric" if 1 == 1 else "alex"
print("==",name)

 

lambda 带if条件的三元运算

 

1
2
3
4
5
6
7
8
l1 = [11,22,33,44,55]
# for i in l1:
#     if i % 2 == 0:
#         m = map(lambda x:x+100,[i,])
#         l1[l1.index(i)] = list(m)[0]
# print(l1)
k=map(lambda x:(x+100) if (x%2==0) else x,l1)
print(list(k))

  

四  深浅拷贝

 1 数字和字符串

对于 数字 和 字符串 而言,赋值、浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址。

 

复制代码
import copy
# ######### 数字、字符串 #########
n1 = 123
# n1 = "i am alex age 10"
print(id(n1))
# ## 赋值 ##
n2 = n1
print(id(n2))
# ## 浅拷贝 ##
n2 = copy.copy(n1)
print(id(n2))
  
# ## 深拷贝 ##
n3 = copy.deepcopy(n1)
print(id(n3))
复制代码

 

二、其他基本数据类型

 

对于字典、元祖、列表 而言,进行赋值、浅拷贝和深拷贝时,其内存地址的变化是不同的。

1、赋值

赋值,只是创建一个变量,该变量指向原来内存地址,如:

n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n2 = n1

 

2、浅拷贝

浅拷贝,在内存中只额外创建第一层数据

import copy
  
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n3 = copy.copy(n1)

 

3、深拷贝

深拷贝,在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化)  最底层数据要满足第一条赋值变量的条件

import copy
  
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n4 = copy.deepcopy(n1)

 

posted on 2018-03-06 15:17  卡子方  阅读(160)  评论(0编辑  收藏  举报

levels of contents