Python基础篇【第五篇】:sed和函数
sed是一个无序且不重复的元素集合
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 '''添加元素''' 11 Add an element to a set. 12 13 This has no effect if the element is already present. 14 """ 15 pass 16 17 def clear(self, *args, **kwargs): # real signature unknown 18 '''清除内容(set中的元素)''' 19 """ Remove all elements from this set. """ 20 pass 21 22 def copy(self, *args, **kwargs): # real signature unknown 23 '''浅拷贝''' 24 """ Return a shallow copy of a set. """ 25 pass 26 27 def difference(self, *args, **kwargs): # real signature unknown 28 '''A中存在B中不存在''' 29 """ 30 Return the difference of two or more sets as a new set. 31 32 (i.e. all elements that are in this set but not the others.) 33 """ 34 pass 35 36 def difference_update(self, *args, **kwargs): # real signature unknown 37 '''从当前的集合中删除B中相同的元素''' 38 """ Remove all elements of another set from this set. """ 39 pass 40 41 def discard(self, *args, **kwargs): # real signature unknown 42 '''移除元素,不存在不报错''' 43 """ 44 Remove an element from a set if it is a member. 45 46 If the element is not a member, do nothing. 47 """ 48 pass 49 50 def intersection(self, *args, **kwargs): # real signature unknown 51 '''交集''' 52 """ 53 Return the intersection of two sets as a new set. 54 55 (i.e. all elements that are in both sets.) 56 """ 57 pass 58 59 def intersection_update(self, *args, **kwargs): # real signature unknown 60 '''取交集并更新到A中''' 61 """ Update a set with the intersection of itself and another. """ 62 pass 63 64 def isdisjoint(self, *args, **kwargs): # real signature unknown 65 '''如果没有交集返回true,否则返回false''' 66 """ Return True if two sets have a null intersection. """ 67 pass 68 69 def issubset(self, *args, **kwargs): # real signature unknown 70 '''是否是子序列''' 71 """ Report whether another set contains this set. """ 72 pass 73 74 def issuperset(self, *args, **kwargs): # real signature unknown 75 '''是否是父序列''' 76 """ Report whether this set contains another set. """ 77 pass 78 79 def pop(self, *args, **kwargs): # real signature unknown 80 '''移除元素,并拿到该元素''' 81 """ 82 Remove and return an arbitrary set element. 83 Raises KeyError if the set is empty. 84 """ 85 pass 86 87 def remove(self, *args, **kwargs): # real signature unknown 88 '''移除指定元素,不存在报错''' 89 """ 90 Remove an element from a set; it must be a member. 91 92 If the element is not a member, raise a KeyError. 93 """ 94 pass 95 96 def symmetric_difference(self, *args, **kwargs): # real signature unknown 97 '''对称交集''' 98 """ 99 Return the symmetric difference of two sets as a new set. 100 101 (i.e. all elements that are in exactly one of the sets.) 102 """ 103 pass 104 105 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown 106 '''对称交集,并更新到a中''' 107 """ Update a set with the symmetric difference of itself and another. """ 108 pass 109 110 def union(self, *args, **kwargs): # real signature unknown 111 '''并集''' 112 """ 113 Return the union of sets as a new set. 114 115 (i.e. all elements that are in either set.) 116 """ 117 pass 118 119 def update(self, *args, **kwargs): # real signature unknown 120 '''更新''' 121 """ Update a set with the union of itself and others. """ 122 pass 123 124 def __and__(self, *args, **kwargs): # real signature unknown 125 """ Return self&value. """ 126 pass 127 128 def __contains__(self, y): # real signature unknown; restored from __doc__ 129 """ x.__contains__(y) <==> y in x. """ 130 pass 131 132 def __eq__(self, *args, **kwargs): # real signature unknown 133 """ Return self==value. """ 134 pass 135 136 def __getattribute__(self, *args, **kwargs): # real signature unknown 137 """ Return getattr(self, name). """ 138 pass 139 140 def __ge__(self, *args, **kwargs): # real signature unknown 141 """ Return self>=value. """ 142 pass 143 144 def __gt__(self, *args, **kwargs): # real signature unknown 145 """ Return self>value. """ 146 pass 147 148 def __iand__(self, *args, **kwargs): # real signature unknown 149 """ Return self&=value. """ 150 pass 151 152 def __init__(self, seq=()): # known special case of set.__init__ 153 """ 154 set() -> new empty set object 155 set(iterable) -> new set object 156 157 Build an unordered collection of unique elements. 158 # (copied from class doc) 159 """ 160 pass 161 162 def __ior__(self, *args, **kwargs): # real signature unknown 163 """ Return self|=value. """ 164 pass 165 166 def __isub__(self, *args, **kwargs): # real signature unknown 167 """ Return self-=value. """ 168 pass 169 170 def __iter__(self, *args, **kwargs): # real signature unknown 171 """ Implement iter(self). """ 172 pass 173 174 def __ixor__(self, *args, **kwargs): # real signature unknown 175 """ Return self^=value. """ 176 pass 177 178 def __len__(self, *args, **kwargs): # real signature unknown 179 """ Return len(self). """ 180 pass 181 182 def __le__(self, *args, **kwargs): # real signature unknown 183 """ Return self<=value. """ 184 pass 185 186 def __lt__(self, *args, **kwargs): # real signature unknown 187 """ Return self<value. """ 188 pass 189 190 @staticmethod # known case of __new__ 191 def __new__(*args, **kwargs): # real signature unknown 192 """ Create and return a new object. See help(type) for accurate signature. """ 193 pass 194 195 def __ne__(self, *args, **kwargs): # real signature unknown 196 """ Return self!=value. """ 197 pass 198 199 def __or__(self, *args, **kwargs): # real signature unknown 200 """ Return self|value. """ 201 pass 202 203 def __rand__(self, *args, **kwargs): # real signature unknown 204 """ Return value&self. """ 205 pass 206 207 def __reduce__(self, *args, **kwargs): # real signature unknown 208 """ Return state information for pickling. """ 209 pass 210 211 def __repr__(self, *args, **kwargs): # real signature unknown 212 """ Return repr(self). """ 213 pass 214 215 def __ror__(self, *args, **kwargs): # real signature unknown 216 """ Return value|self. """ 217 pass 218 219 def __rsub__(self, *args, **kwargs): # real signature unknown 220 """ Return value-self. """ 221 pass 222 223 def __rxor__(self, *args, **kwargs): # real signature unknown 224 """ Return value^self. """ 225 pass 226 227 def __sizeof__(self): # real signature unknown; restored from __doc__ 228 """ S.__sizeof__() -> size of S in memory, in bytes """ 229 pass 230 231 def __sub__(self, *args, **kwargs): # real signature unknown 232 """ Return self-value. """ 233 pass 234 235 def __xor__(self, *args, **kwargs): # real signature unknown 236 """ Return self^value. """ 237 pass 238 239 __hash__ = None
以上常用实例:
1 #!/usr/bin/env python3 2 3 # se = {11,22,33} 4 # be = {55} 5 6 # #增加 7 # se.add(44) 8 # print(se) 9 10 # 更新 11 # se.update(be) 12 # print(se) 13 14 # #清除 15 # se.clear() 16 # print(se) 17 18 # #找se中存在的be不存在的 19 # se.difference(be) 20 # ret = se.difference(be) 21 # print(se) 22 # print(ret) 23 24 25 # #找se中存在,be中不存在的集合,更新自己 26 # se.difference_update(be) 27 # print(se) 28 29 # #移除指定的元素,不存在不报错 30 # se.discard(11) 31 # print(se) 32 33 # #移除元素,不存在会报错 34 # se.remove(11) 35 # print(se) 36 37 # #取交集 38 # ret = se.intersection(be) 39 # print(ret) 40 41 # #取交集并更新 42 # se.intersection_update(be) 43 # print(se) 44 45 # #判断交集有false,没有true 46 # ret = se.isdisjoint(be) 47 # print(ret) 48 49 #子序列,父序列,谁多谁是父亲,谁少谁是儿子 50 # ret = se.issubset(be) 51 # print(ret) 52 # ret = se.issuperset(be) 53 # print(ret) 54 55 #移除 56 # ret = se.pop() 57 # print(ret) 58 59 #对称交集 60 # set = se.symmetric_difference(be) 61 # print(set) 62 63 # #对称交集更新 64 # se.symmetric_difference_update(be) 65 # print(se) 66 67 #并集 68 # ret = se.union(be) 69 # print(ret) 70 71 # se = {11,22,33} 72 # be = {22,55} 73 # for i in se: 74 # print(se) 75 # 76 # ret = 11 in se 77 # print(ret)
深、浅copy
1、深拷贝是在另一块地址中创建一个新的变量或容器,同时容器内的元素的地址也是新开辟的,仅仅是值相同而已,是完全的副本。
2、浅拷贝是在另一块地址中创建一个新的变量或容器,但是容器内的元素的地址均是源对象的元素的地址的拷贝。也就是说新的容器中指向了旧的元素。
函数
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。
函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数。
定义函数:
- 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 ()。
- 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数。
- 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。
- 函数内容以冒号起始,并且缩进。
- return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。
语法:
1 def 函数名字(形参): 2 函数体
默认情况下,参数值和参数名称是按函数声明中定义的的顺序匹配起来的。
实例:
1 def hello(): 2 print('hello~')
调用函数:
1 #!/usr/bin/env python3 2 3 def test(str): 4 print(str) 5 return6 7 print("调用")