目录:
1.集合set
2.计数器
3.有序字典
4.默认字典
5.可命名元组
6.队列
7.深浅拷贝
8.函数
9.lambda表达式
10.内置函数
一、集合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 """ 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 """ Remove all elements from this set. """ 19 pass 20 21 def copy(self, *args, **kwargs): # real signature unknown 22 """ Return a shallow copy of a set. """ 23 pass 24 25 def difference(self, *args, **kwargs): # real signature unknown 26 """ 27 Return the difference of two or more sets as a new set. 28 29 (i.e. all elements that are in this set but not the others.) 30 """ 31 pass 32 33 def difference_update(self, *args, **kwargs): # real signature unknown 34 """ 删除当前set中的所有包含在 new set 里的元素 """ 35 """ Remove all elements of another set from this set. """ 36 pass 37 38 def discard(self, *args, **kwargs): # real signature unknown 39 """ 移除元素 """ 40 """ 41 Remove an element from a set if it is a member. 42 43 If the element is not a member, do nothing. 44 """ 45 pass 46 47 def intersection(self, *args, **kwargs): # real signature unknown 48 """ 取交集,新创建一个set """ 49 """ 50 Return the intersection of two or more sets as a new set. 51 52 (i.e. elements that are common to all of the sets.) 53 """ 54 pass 55 56 def intersection_update(self, *args, **kwargs): # real signature unknown 57 """ 取交集,修改原来set """ 58 """ Update a set with the intersection of itself and another. """ 59 pass 60 61 def isdisjoint(self, *args, **kwargs): # real signature unknown 62 """ 如果没有交集,返回true """ 63 """ Return True if two sets have a null intersection. """ 64 pass 65 66 def issubset(self, *args, **kwargs): # real signature unknown 67 """ 是否是子集 """ 68 """ Report whether another set contains this set. """ 69 pass 70 71 def issuperset(self, *args, **kwargs): # real signature unknown 72 """ 是否是父集 """ 73 """ Report whether this set contains another set. """ 74 pass 75 76 def pop(self, *args, **kwargs): # real signature unknown 77 """ 移除 """ 78 """ 79 Remove and return an arbitrary set element. 80 Raises KeyError if the set is empty. 81 """ 82 pass 83 84 def remove(self, *args, **kwargs): # real signature unknown 85 """ 移除 """ 86 """ 87 Remove an element from a set; it must be a member. 88 89 If the element is not a member, raise a KeyError. 90 """ 91 pass 92 93 def symmetric_difference(self, *args, **kwargs): # real signature unknown 94 """ 差集,创建新对象""" 95 """ 96 Return the symmetric difference of two sets as a new set. 97 98 (i.e. all elements that are in exactly one of the sets.) 99 """ 100 pass 101 102 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown 103 """ 差集,改变原来 """ 104 """ Update a set with the symmetric difference of itself and another. """ 105 pass 106 107 def union(self, *args, **kwargs): # real signature unknown 108 """ 并集 """ 109 """ 110 Return the union of sets as a new set. 111 112 (i.e. all elements that are in either set.) 113 """ 114 pass 115 116 def update(self, *args, **kwargs): # real signature unknown 117 """ 更新 """ 118 """ Update a set with the union of itself and others. """ 119 pass 120 121 def __and__(self, y): # real signature unknown; restored from __doc__ 122 """ x.__and__(y) <==> x&y """ 123 pass 124 125 def __cmp__(self, y): # real signature unknown; restored from __doc__ 126 """ x.__cmp__(y) <==> cmp(x,y) """ 127 pass 128 129 def __contains__(self, y): # real signature unknown; restored from __doc__ 130 """ x.__contains__(y) <==> y in x. """ 131 pass 132 133 def __eq__(self, y): # real signature unknown; restored from __doc__ 134 """ x.__eq__(y) <==> x==y """ 135 pass 136 137 def __getattribute__(self, name): # real signature unknown; restored from __doc__ 138 """ x.__getattribute__('name') <==> x.name """ 139 pass 140 141 def __ge__(self, y): # real signature unknown; restored from __doc__ 142 """ x.__ge__(y) <==> x>=y """ 143 pass 144 145 def __gt__(self, y): # real signature unknown; restored from __doc__ 146 """ x.__gt__(y) <==> x>y """ 147 pass 148 149 def __iand__(self, y): # real signature unknown; restored from __doc__ 150 """ x.__iand__(y) <==> x&=y """ 151 pass 152 153 def __init__(self, seq=()): # known special case of set.__init__ 154 """ 155 set() -> new empty set object 156 set(iterable) -> new set object 157 158 Build an unordered collection of unique elements. 159 # (copied from class doc) 160 """ 161 pass 162 163 def __ior__(self, y): # real signature unknown; restored from __doc__ 164 """ x.__ior__(y) <==> x|=y """ 165 pass 166 167 def __isub__(self, y): # real signature unknown; restored from __doc__ 168 """ x.__isub__(y) <==> x-=y """ 169 pass 170 171 def __iter__(self): # real signature unknown; restored from __doc__ 172 """ x.__iter__() <==> iter(x) """ 173 pass 174 175 def __ixor__(self, y): # real signature unknown; restored from __doc__ 176 """ x.__ixor__(y) <==> x^=y """ 177 pass 178 179 def __len__(self): # real signature unknown; restored from __doc__ 180 """ x.__len__() <==> len(x) """ 181 pass 182 183 def __le__(self, y): # real signature unknown; restored from __doc__ 184 """ x.__le__(y) <==> x<=y """ 185 pass 186 187 def __lt__(self, y): # real signature unknown; restored from __doc__ 188 """ x.__lt__(y) <==> x<y """ 189 pass 190 191 @staticmethod # known case of __new__ 192 def __new__(S, *more): # real signature unknown; restored from __doc__ 193 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ 194 pass 195 196 def __ne__(self, y): # real signature unknown; restored from __doc__ 197 """ x.__ne__(y) <==> x!=y """ 198 pass 199 200 def __or__(self, y): # real signature unknown; restored from __doc__ 201 """ x.__or__(y) <==> x|y """ 202 pass 203 204 def __rand__(self, y): # real signature unknown; restored from __doc__ 205 """ x.__rand__(y) <==> y&x """ 206 pass 207 208 def __reduce__(self, *args, **kwargs): # real signature unknown 209 """ Return state information for pickling. """ 210 pass 211 212 def __repr__(self): # real signature unknown; restored from __doc__ 213 """ x.__repr__() <==> repr(x) """ 214 pass 215 216 def __ror__(self, y): # real signature unknown; restored from __doc__ 217 """ x.__ror__(y) <==> y|x """ 218 pass 219 220 def __rsub__(self, y): # real signature unknown; restored from __doc__ 221 """ x.__rsub__(y) <==> y-x """ 222 pass 223 224 def __rxor__(self, y): # real signature unknown; restored from __doc__ 225 """ x.__rxor__(y) <==> y^x """ 226 pass 227 228 def __sizeof__(self): # real signature unknown; restored from __doc__ 229 """ S.__sizeof__() -> size of S in memory, in bytes """ 230 pass 231 232 def __sub__(self, y): # real signature unknown; restored from __doc__ 233 """ x.__sub__(y) <==> x-y """ 234 pass 235 236 def __xor__(self, y): # real signature unknown; restored from __doc__ 237 """ x.__xor__(y) <==> x^y """ 238 pass 239 240 __hash__ = None 241 242 set
二、计数器(counter)
Counter是对字典类型的补充,用于追踪值出现的次数
ps:具备字典的所有功能+自己的功能
c = Counter('abcdeabcdabcaba') print c 输出:Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
1 ######################################################################## 2 ### Counter 3 ######################################################################## 4 5 class Counter(dict): 6 '''Dict subclass for counting hashable items. Sometimes called a bag 7 or multiset. Elements are stored as dictionary keys and their counts 8 are stored as dictionary values. 9 10 >>> c = Counter('abcdeabcdabcaba') # count elements from a string 11 12 >>> c.most_common(3) # three most common elements 13 [('a', 5), ('b', 4), ('c', 3)] 14 >>> sorted(c) # list all unique elements 15 ['a', 'b', 'c', 'd', 'e'] 16 >>> ''.join(sorted(c.elements())) # list elements with repetitions 17 'aaaaabbbbcccdde' 18 >>> sum(c.values()) # total of all counts 19 20 >>> c['a'] # count of letter 'a' 21 >>> for elem in 'shazam': # update counts from an iterable 22 ... c[elem] += 1 # by adding 1 to each element's count 23 >>> c['a'] # now there are seven 'a' 24 >>> del c['b'] # remove all 'b' 25 >>> c['b'] # now there are zero 'b' 26 27 >>> d = Counter('simsalabim') # make another counter 28 >>> c.update(d) # add in the second counter 29 >>> c['a'] # now there are nine 'a' 30 31 >>> c.clear() # empty the counter 32 >>> c 33 Counter() 34 35 Note: If a count is set to zero or reduced to zero, it will remain 36 in the counter until the entry is deleted or the counter is cleared: 37 38 >>> c = Counter('aaabbc') 39 >>> c['b'] -= 2 # reduce the count of 'b' by two 40 >>> c.most_common() # 'b' is still in, but its count is zero 41 [('a', 3), ('c', 1), ('b', 0)] 42 43 ''' 44 # References: 45 # http://en.wikipedia.org/wiki/Multiset 46 # http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html 47 # http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm 48 # http://code.activestate.com/recipes/259174/ 49 # Knuth, TAOCP Vol. II section 4.6.3 50 51 def __init__(self, iterable=None, **kwds): 52 '''Create a new, empty Counter object. And if given, count elements 53 from an input iterable. Or, initialize the count from another mapping 54 of elements to their counts. 55 56 >>> c = Counter() # a new, empty counter 57 >>> c = Counter('gallahad') # a new counter from an iterable 58 >>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping 59 >>> c = Counter(a=4, b=2) # a new counter from keyword args 60 61 ''' 62 super(Counter, self).__init__() 63 self.update(iterable, **kwds) 64 65 def __missing__(self, key): 66 """ 对于不存在的元素,返回计数器为0 """ 67 'The count of elements not in the Counter is zero.' 68 # Needed so that self[missing_item] does not raise KeyError 69 return 0 70 71 def most_common(self, n=None): 72 """ 数量大于等n的所有元素和计数器 """ 73 '''List the n most common elements and their counts from the most 74 common to the least. If n is None, then list all element counts. 75 76 >>> Counter('abcdeabcdabcaba').most_common(3) 77 [('a', 5), ('b', 4), ('c', 3)] 78 79 ''' 80 # Emulate Bag.sortedByCount from Smalltalk 81 if n is None: 82 return sorted(self.iteritems(), key=_itemgetter(1), reverse=True) 83 return _heapq.nlargest(n, self.iteritems(), key=_itemgetter(1)) 84 85 def elements(self): 86 """ 计数器中的所有元素,注:此处非所有元素集合,而是包含所有元素集合的迭代器 """ 87 '''Iterator over elements repeating each as many times as its count. 88 89 >>> c = Counter('ABCABC') 90 >>> sorted(c.elements()) 91 ['A', 'A', 'B', 'B', 'C', 'C'] 92 93 # Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1 94 >>> prime_factors = Counter({2: 2, 3: 3, 17: 1}) 95 >>> product = 1 96 >>> for factor in prime_factors.elements(): # loop over factors 97 ... product *= factor # and multiply them 98 >>> product 99 100 Note, if an element's count has been set to zero or is a negative 101 number, elements() will ignore it. 102 103 ''' 104 # Emulate Bag.do from Smalltalk and Multiset.begin from C++. 105 return _chain.from_iterable(_starmap(_repeat, self.iteritems())) 106 107 # Override dict methods where necessary 108 109 @classmethod 110 def fromkeys(cls, iterable, v=None): 111 # There is no equivalent method for counters because setting v=1 112 # means that no element can have a count greater than one. 113 raise NotImplementedError( 114 'Counter.fromkeys() is undefined. Use Counter(iterable) instead.') 115 116 def update(self, iterable=None, **kwds): 117 """ 更新计数器,其实就是增加;如果原来没有,则新建,如果有则加一 """ 118 '''Like dict.update() but add counts instead of replacing them. 119 120 Source can be an iterable, a dictionary, or another Counter instance. 121 122 >>> c = Counter('which') 123 >>> c.update('witch') # add elements from another iterable 124 >>> d = Counter('watch') 125 >>> c.update(d) # add elements from another counter 126 >>> c['h'] # four 'h' in which, witch, and watch 127 128 ''' 129 # The regular dict.update() operation makes no sense here because the 130 # replace behavior results in the some of original untouched counts 131 # being mixed-in with all of the other counts for a mismash that 132 # doesn't have a straight-forward interpretation in most counting 133 # contexts. Instead, we implement straight-addition. Both the inputs 134 # and outputs are allowed to contain zero and negative counts. 135 136 if iterable is not None: 137 if isinstance(iterable, Mapping): 138 if self: 139 self_get = self.get 140 for elem, count in iterable.iteritems(): 141 self[elem] = self_get(elem, 0) + count 142 else: 143 super(Counter, self).update(iterable) # fast path when counter is empty 144 else: 145 self_get = self.get 146 for elem in iterable: 147 self[elem] = self_get(elem, 0) + 1 148 if kwds: 149 self.update(kwds) 150 151 def subtract(self, iterable=None, **kwds): 152 """ 相减,原来的计数器中的每一个元素的数量减去后添加的元素的数量 """ 153 '''Like dict.update() but subtracts counts instead of replacing them. 154 Counts can be reduced below zero. Both the inputs and outputs are 155 allowed to contain zero and negative counts. 156 157 Source can be an iterable, a dictionary, or another Counter instance. 158 159 >>> c = Counter('which') 160 >>> c.subtract('witch') # subtract elements from another iterable 161 >>> c.subtract(Counter('watch')) # subtract elements from another counter 162 >>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch 163 >>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch 164 -1 165 166 ''' 167 if iterable is not None: 168 self_get = self.get 169 if isinstance(iterable, Mapping): 170 for elem, count in iterable.items(): 171 self[elem] = self_get(elem, 0) - count 172 else: 173 for elem in iterable: 174 self[elem] = self_get(elem, 0) - 1 175 if kwds: 176 self.subtract(kwds) 177 178 def copy(self): 179 """ 拷贝 """ 180 'Return a shallow copy.' 181 return self.__class__(self) 182 183 def __reduce__(self): 184 """ 返回一个元组(类型,元组) """ 185 return self.__class__, (dict(self),) 186 187 def __delitem__(self, elem): 188 """ 删除元素 """ 189 'Like dict.__delitem__() but does not raise KeyError for missing values.' 190 if elem in self: 191 super(Counter, self).__delitem__(elem) 192 193 def __repr__(self): 194 if not self: 195 return '%s()' % self.__class__.__name__ 196 items = ', '.join(map('%r: %r'.__mod__, self.most_common())) 197 return '%s({%s})' % (self.__class__.__name__, items) 198 199 # Multiset-style mathematical operations discussed in: 200 # Knuth TAOCP Volume II section 4.6.3 exercise 19 201 # and at http://en.wikipedia.org/wiki/Multiset 202 # 203 # Outputs guaranteed to only include positive counts. 204 # 205 # To strip negative and zero counts, add-in an empty counter: 206 # c += Counter() 207 208 def __add__(self, other): 209 '''Add counts from two counters. 210 211 >>> Counter('abbb') + Counter('bcc') 212 Counter({'b': 4, 'c': 2, 'a': 1}) 213 214 ''' 215 if not isinstance(other, Counter): 216 return NotImplemented 217 result = Counter() 218 for elem, count in self.items(): 219 newcount = count + other[elem] 220 if newcount > 0: 221 result[elem] = newcount 222 for elem, count in other.items(): 223 if elem not in self and count > 0: 224 result[elem] = count 225 return result 226 227 def __sub__(self, other): 228 ''' Subtract count, but keep only results with positive counts. 229 230 >>> Counter('abbbc') - Counter('bccd') 231 Counter({'b': 2, 'a': 1}) 232 233 ''' 234 if not isinstance(other, Counter): 235 return NotImplemented 236 result = Counter() 237 for elem, count in self.items(): 238 newcount = count - other[elem] 239 if newcount > 0: 240 result[elem] = newcount 241 for elem, count in other.items(): 242 if elem not in self and count < 0: 243 result[elem] = 0 - count 244 return result 245 246 def __or__(self, other): 247 '''Union is the maximum of value in either of the input counters. 248 249 >>> Counter('abbb') | Counter('bcc') 250 Counter({'b': 3, 'c': 2, 'a': 1}) 251 252 ''' 253 if not isinstance(other, Counter): 254 return NotImplemented 255 result = Counter() 256 for elem, count in self.items(): 257 other_count = other[elem] 258 newcount = other_count if count < other_count else count 259 if newcount > 0: 260 result[elem] = newcount 261 for elem, count in other.items(): 262 if elem not in self and count > 0: 263 result[elem] = count 264 return result 265 266 def __and__(self, other): 267 ''' Intersection is the minimum of corresponding counts. 268 269 >>> Counter('abbb') & Counter('bcc') 270 Counter({'b': 1}) 271 272 ''' 273 if not isinstance(other, Counter): 274 return NotImplemented 275 result = Counter() 276 for elem, count in self.items(): 277 other_count = other[elem] 278 newcount = count if count < other_count else other_count 279 if newcount > 0: 280 result[elem] = newcount 281 return result 282 283 Counter 284 285 Counter
三、有序字典
orderdDict是对字典类型的补充,他记住了字典元素添加的顺序
例:
dic = collections.OrderedDict() dic['k1']='v1' dic['k2']='v2' dic['k3']='v3' print(dic)
操作方法与字典操作方法相同
四、默认字典
dic=collections.defaultdict(list) dic['k1'].append('cat') print(dic)
操作方法与字典操作方法相同
五、可命名元组(namedtuple)
根据nametuple可以创建一个包含tuple所有功能以及其他功能的类型。
import collections Mytuple = collections.namedtuple('Mytuple',['x', 'y', 'z'])
六、队列
双向队列(deque):
d = collections.deque() d.append('1') #添加数据到队列 d.appendleft('10') #添加数据到队列左边 d.appendleft('1') print(d) r = d.count('1') #查看1在队列里的个数 print(r) d.extend(['yy','uu','ii']) #添加多个元素到列表 d.extendleft(['y1y','u1u','i1i']) #添加多个元素到列表左边 print(d) d.rotate(5) #将右边的值按顺序放到左边,括号里的值为要取值的个数 print(d)
单向队列(queue):
import queue q = queue.Queue() q.put('123') q.put('678') #将数据添加到队列 print(q.qsize()) #获取队列中数据的个数 print(q.get()) #拿取队列中的数据(先进先出原则)
七、深浅拷贝
对于 数字 和 字符串 而言,赋值、浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址。
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))
对于字典、元祖、列表 而言,进行赋值、浅拷贝和深拷贝时,其内存地址的变化是不同的。
赋值,只是创建一个变量,该变量指向原来内存地址,如:
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]} n2 = n1
浅拷贝,在内存中只额外创建第一层数据
import copy n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]} n3 = copy.copy(n1)
深拷贝,在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化)
import copy n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]} n4 = copy.deepcopy(n1)
八、函数
一、背景
在学习函数之前,一直遵循:面向过程编程,即:根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复制到现需功能处,如下:
while True: if cpu利用率 > 90%: #发送邮件提醒 连接邮箱服务器 发送邮件 关闭连接 if 硬盘使用空间 > 90%: #发送邮件提醒 连接邮箱服务器 发送邮件 关闭连接 if 内存占用 > 80%: #发送邮件提醒 连接邮箱服务器 发送邮件 关闭连接
仔细一看上述代码,if条件语句下的内容可以被提取出来公用,如下:
def 发送邮件(内容) #发送邮件提醒 连接邮箱服务器 发送邮件 关闭连接 while True: if cpu利用率 > 90%: 发送邮件('CPU报警') if 硬盘使用空间 > 90%: 发送邮件('硬盘报警') if 内存占用 > 80%:
对于上述的两种实现方式,第二次必然比第一次的重用性和可读性要好,其实这就是函数式编程和面向过程编程的区别:
- 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可
- 面向对象:对函数进行分类和封装,让开发“更快更好更强...”
函数式编程最重要的是增强代码的重用性和可读性
二、定义和使用
def 函数名(参数): ... 函数体 ...
函数的定义主要有如下要点:
- def:表示函数的关键字
- 函数名:函数的名称,日后根据函数名调用函数
- 函数体:函数中进行一系列的逻辑计算,如:发送邮件、计算出 [11,22,38,888,2]中的最大数等...
- 参数:为函数体提供数据
- 返回值:当函数执行完毕后,可以给调用者返回数据。
以上要点中,比较重要有参数和返回值:
1、返回值
函数是一个功能块,该功能到底执行成功与否,需要通过返回值来告知调用者。
def 发送短信(): 发送短信的代码... if 发送成功: return True else: return False while True: # 每次执行发送短信函数,都会将返回值自动赋值给result # 之后,可以根据result来写日志,或重发等操作 result = 发送短信() if result == False: 记录日志,短信发送失败...
2、参数
为什么要有参数?
1 def 发送邮件(邮件内容) 2 3 #发送邮件提醒 4 连接邮箱服务器 5 发送邮件 6 关闭连接 7 8 9 while True: 10 11 if cpu利用率 > 90%: 12 发送邮件("CPU报警了。") 13 14 if 硬盘使用空间 > 90%: 15 发送邮件("硬盘报警了。") 16 17 if 内存占用 > 80%: 18 发送邮件("内存报警了。")
1 def CPU报警邮件() 2 #发送邮件提醒 3 连接邮箱服务器 4 发送邮件 5 关闭连接 6 7 def 硬盘报警邮件() 8 #发送邮件提醒 9 连接邮箱服务器 10 发送邮件 11 关闭连接 12 13 def 内存报警邮件() 14 #发送邮件提醒 15 连接邮箱服务器 16 发送邮件 17 关闭连接 18 19 while True: 20 21 if cpu利用率 > 90%: 22 CPU报警邮件() 23 24 if 硬盘使用空间 > 90%: 25 硬盘报警邮件() 26 27 if 内存占用 > 80%: 28 内存报警邮件()
函数的有三中不同的参数:
- 普通参数
- 默认参数
- 动态参数
1 # ######### 定义函数 ######### 2 3 # name 叫做函数func的形式参数,简称:形参 4 def func(name): 5 print name 6 7 # ######### 执行函数 ######### 8 # 'spykdis' 叫做函数func的实际参数,简称:实参 9 func('spykdis')
1 def func(name, age = 18): 2 3 print "%s:%s" %(name,age) 4 5 # 指定参数 6 func('cat', 19) 7 # 使用默认参数 8 func('tom') 9 10 注:默认参数需要放在参数列表最后
1 def func(*args): 2 3 print args 4 5 6 # 执行方式一 7 func(11,33,4,4454,5) 8 9 # 执行方式二 10 li = [11,2,2,3,3,4,54] 11 func(*li)
1 def func(**kwargs): 2 3 print args 4 5 6 # 执行方式一 7 func(name='tom',age=18) 8 9 # 执行方式二 10 li = {'name':'tom', age:18, 'gender':'male'} 11 func(**li)
1 def func(*args, **kwargs): 2 3 print args 4 print kwargs
九、lambda表达式
学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即:
# 普通条件语句 if 1 == 1: name = 'wupeiqi' else: name = 'alex' # 三元运算 name = 'wupeiqi' if 1 == 1 else 'alex'
对于简单的函数,也存在一种简便的表示方式,即:lambda表达式
# ###################### 普通函数 ###################### # 定义函数(普通方式) def func(arg): return arg + 1 # 执行函数 result = func(123) # ###################### lambda ###################### # 定义函数(lambda表达式) my_lambda = lambda arg : arg + 1 # 执行函数 result = my_lambda(123)
lambda存在意义就是对简单函数的简洁表示
十、内置函数
详细见python文档,猛击这里
map
遍历序列,对序列中每个元素进行操作,最终获取新的序列。
1 li = [11, 22, 33] 2 3 new_list = map(lambda a: a + 100, li)
1 li = [11, 22, 33] 2 sl = [1, 2, 3] 3 new_list = map(lambda a, b: a + b, li, sl)
filter
对于序列中的元素进行筛选,最终获取符合条件的序列
1 li = [11, 22, 33] 2 3 new_list = filter(lambda arg: arg > 22, li) 4 5 #filter第一个参数为空,将获取原来序列
reduce
对于序列内所有元素进行累计操作
1 li = [11, 22, 33] 2 3 result = reduce(lambda arg1, arg2: arg1 + arg2, li) 4 5 # reduce的第一个参数,函数必须要有两个参数 6 # reduce的第二个参数,要循环的序列 7 # reduce的第三个参数,初始值