三元运算

三元运算(三目运算),是对简单的条件语句的缩写。

1 # 书写格式
2 result = 值1 if 条件 else 值2
3 # 如果条件成立,那么将 “值1” 赋值给result变量,否则,将“值2”赋值给result变量

基本数据类型补充

set

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

  1 class set(object):
  2     """
  3     set() -> new empty set object
  4     set(iterable) -> new set object
  5      
  6     Build an unordered collection of unique elements.
  7     """
  8     def add(self, *args, **kwargs): # real signature unknown
  9         """
 10         Add an element to a set,添加元素
 11          
 12         This has no effect if the element is already present.
 13         """
 14         pass
 15  
 16     def clear(self, *args, **kwargs): # real signature unknown
 17         """ Remove all elements from this set. 清除内容"""
 18         pass
 19  
 20     def copy(self, *args, **kwargs): # real signature unknown
 21         """ Return a shallow copy of a set. 浅拷贝  """
 22         pass
 23  
 24     def difference(self, *args, **kwargs): # real signature unknown
 25         """
 26         Return the difference of two or more sets as a new set. A中存在,B中不存在
 27          
 28         (i.e. all elements that are in this set but not the others.)
 29         """
 30         pass
 31  
 32     def difference_update(self, *args, **kwargs): # real signature unknown
 33         """ Remove all elements of another set from this set.  从当前集合中删除和B中相同的元素"""
 34         pass
 35  
 36     def discard(self, *args, **kwargs): # real signature unknown
 37         """
 38         Remove an element from a set if it is a member.
 39          
 40         If the element is not a member, do nothing. 移除指定元素,不存在不保错
 41         """
 42         pass
 43  
 44     def intersection(self, *args, **kwargs): # real signature unknown
 45         """
 46         Return the intersection of two sets as a new set. 交集
 47          
 48         (i.e. all elements that are in both sets.)
 49         """
 50         pass
 51  
 52     def intersection_update(self, *args, **kwargs): # real signature unknown
 53         """ Update a set with the intersection of itself and another.  取交集并更更新到A中 """
 54         pass
 55  
 56     def isdisjoint(self, *args, **kwargs): # real signature unknown
 57         """ Return True if two sets have a null intersection.  如果没有交集,返回True,否则返回False"""
 58         pass
 59  
 60     def issubset(self, *args, **kwargs): # real signature unknown
 61         """ Report whether another set contains this set.  是否是子序列"""
 62         pass
 63  
 64     def issuperset(self, *args, **kwargs): # real signature unknown
 65         """ Report whether this set contains another set. 是否是父序列"""
 66         pass
 67  
 68     def pop(self, *args, **kwargs): # real signature unknown
 69         """
 70         Remove and return an arbitrary set element.
 71         Raises KeyError if the set is empty. 移除元素
 72         """
 73         pass
 74  
 75     def remove(self, *args, **kwargs): # real signature unknown
 76         """
 77         Remove an element from a set; it must be a member.
 78          
 79         If the element is not a member, raise a KeyError. 移除指定元素,不存在保错
 80         """
 81         pass
 82  
 83     def symmetric_difference(self, *args, **kwargs): # real signature unknown
 84         """
 85         Return the symmetric difference of two sets as a new set.  对称差集
 86          
 87         (i.e. all elements that are in exactly one of the sets.)
 88         """
 89         pass
 90  
 91     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
 92         """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
 93         pass
 94  
 95     def union(self, *args, **kwargs): # real signature unknown
 96         """
 97         Return the union of sets as a new set.  并集
 98          
 99         (i.e. all elements that are in either set.)
100         """
101         pass
102  
103     def update(self, *args, **kwargs): # real signature unknown
104         """ Update a set with the union of itself and others. 更新 """
105         pass

练习:寻找差异

old_dict={
"#1":22,
"#2":33,
"#3":55
}
new_dict={
"#1":33,
"#4":44,
"#7":77
}
s1=set(old_dict.items())#将字典变成集合
s2=set(new_dict.items())
tmp=s1.difference(s2) #将s1中存在,s2中不存在的找出来
s1.difference_update(tmp)#将s1中的tmp集合删除掉
s1.update(s2)#将s2集合更新进s1
old_dict=dict(s1)
print(s1)

需要删除:?
需要新建:?
需要更新:?
注意:无需考虑内部元素是否改变,只要原来存在,新汇报也存在,就是需要更新

 1 old_dict={
 2     "#1":22,
 3     "#2":33,
 4     "#3":55
 5 }
 6 new_dict={
 7     "#1":33,
 8     "#4":44,
 9     "#7":77
10 }
11 s1=set(old_dict.items())#将字典变成集合
12 s2=set(new_dict.items())
13 tmp=s1.difference(s2) #将s1中存在,s2中不存在的找出来
14 s1.difference_update(tmp)#将s1中的tmp集合删除掉
15 s1.update(s2)#将s2集合更新进s1
16 old_dict=dict(s1)
17 print(s1)

深浅拷贝

一、数字和字符串

在python中,字符串是一次性创建,不能被修改,只要修改都是再创建;list是链表形式,可以修改。

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

 1 import copy
 2 # ######### 数字、字符串 #########
 3 n1 = 123
 4 # n1 = "i am alex age 10"
 5 print(id(n1))
 6 # ## 赋值 ##
 7 n2 = n1
 8 print(id(n2)) #n1和n2地址是一样的
 9 # ## 浅拷贝 ##
10 n2 = copy.copy(n1)
11 print(id(n2)) #n1和n2地址是一样的
12   
13 # ## 深拷贝 ##
14 n3 = copy.deepcopy(n1)
15 print(id(n3)) #n1和n2地址是一样的

二、其他基本数据类型

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

1、赋值

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

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

  

2、浅拷贝

浅拷贝,在内存中只额外创建第一层数据,只是最外层内存地址变化

import copy
  
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n3 = copy.copy(n1) #此时n1和n3的索引id不一样,但是两个列表中的元素的地址是一样的

 

拷贝:可以理解为复制。如上:将n1 的内存地址拷贝一份。然后跟n3关联起来。但是拷贝的时候只是第一层。(这个层数:可以根据数据的深度来定,比如字典,那就有两层)

3、深拷贝

深拷贝,在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化)

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

通过上面的深浅拷贝,可以知道,拷贝深度不同,最终在改变n1和n3的值时候,带来的结果会有所不一样的

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

n1['k3'][0]="aaaa"
print(n1) {"k1": "wwww", "k2": 123, "k3": ["aaaa", 456]} 

print(n4) {"k1": "wu", "k2": 123, "k3": ["aaaaa", 456]}
 
n4 = copy.deepcopy(n1) 
n1[
'k1']="wwww"
n1['k3'][0]="aaaa" 
print(n1) {"k1": "wwww", "k2": 123, "k3": ["aaaa", 456]} 

print(n4) {"k1": "wu", "k2": 123, "k3": ["alex", 456]}

 

posted on 2017-05-15 17:21  进_进  阅读(135)  评论(0)    收藏  举报