Python学习之路——day03,,九、collecti
Python学习之路——day03,,九、collecti
九、collection系统
先做个练习
练习一:元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {‘k1‘: 大于66 , ‘k2‘: 小于66}
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 ‘‘‘ 4 将一个数字列表分割成字典,‘k1‘的值为小于66的值,‘k2‘的值为大于66的值 5 ‘‘‘ 6 li = [11,22,33,44,55,66,77,88,99,90] 7 dic = {} 8 for ele in li: 9 #先判断是否小于6610 if ele < 66:11 #判断是否有‘k1‘的键,如果有就追加12 if ‘k1‘ in dic.keys():13 dic[‘k1‘].append(ele)14 else:15 #创建只有一个元素的列表16 dic[‘k1‘] = [ele,]17 else:18 if ‘k2‘ in dic.keys():19 dic[‘k2‘].append(ele);20 else:21 dic[‘k2‘] = [ele,]22 23 print dic24 25 #打印出字典的键和值,值占4个字符,前面用’*‘填充,打印时要把数值转换成字符串形式26 for k in dic.keys():27 print k28 if type(dic[k]) == list:29 for i in dic[k]:30 print str(i).rjust(4,‘*‘)dict_Category
练习二:将文件中读出的内容转换成字典
已知文件内容为
alex|123|1eric|123|1john|123|1
转换成字典形式
dic = {
‘alex‘:[123,1]
‘eric‘:[123,1]
‘john‘:[123,1]
}
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 4 ‘‘‘ 5 想要得到的效果 6 dic = { 7 ‘alex‘:[123,1] 8 ‘eric‘:[123,1] 9 ‘john‘:[123,1]10 }11 ‘‘‘12 13 #打开文件,读取文件内容,用的with as,这样可以保证最后文件会被关闭14 with open(‘log.txt‘) as f:15 #将文件读出来赋值给一个列表16 line_list = f.readlines();17 print line_list18 dic = {}19 #遍历列表,将每行再分割成列表20 for line in line_list:21 line = line.strip();22 ele_list = line.split(‘|‘)23 #把姓名做成字典的key,并把后面所有元素当做这个key的值,这里用的是[1:],取出除了0外,所有的值24 dic[ele_list[0]] = ele_list[1:]25 26 #下面是遍历字典的两种方法,推荐使用第一种,因为在字典内容少的时候两种方法都可以27 #但当字典内容多的时候,第二种方法的字典会先值转换成列表,会消耗时间和占用内存28 #方法一29 for k in dic.keys():30 print k,dic[k]31 32 for ele in dic:33 print ele,dic[ele];34 35 #方法二 36 for k,v in dic.items():37 print k,vdict_file
1、计数器(counter)
Counter是对字典类型的补充,用于追踪值的出现次数
ps:具备字典的所有功能+自己的功能
1 >>> import collections2 NameError: name ‘Counter‘ is not defined3 >>> c = collections.Counter(‘asdfasdfasdfasdf‘)4 >>> c5 Counter({‘a‘: 4, ‘s‘: 4, ‘d‘: 4, ‘f‘: 4})6 >>>
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 >>> c 68 Counter({‘a‘: 12, ‘f‘: 10, ‘d‘: 9, ‘s‘: 9, ‘c‘: 3, ‘g‘: 3, ‘e‘: 1}) 69 >>> c[‘a‘] 70 12 71 >>> c[‘z‘] 72 0 73 #原生字典会抛异常 74 >>> a = {1:2,3:4} 75 >>> a[1] 76 2 77 >>> a[55] 78 Traceback (most recent call last): 79 File "<stdin>", line 1, in <module> 80 KeyError: 55 81 >>> 82 """ 83 ‘The count of elements not in the Counter is zero.‘ 84 # Needed so that self[missing_item] does not raise KeyError 85 return 0 86 87 def most_common(self, n=None): 88 """ 列出n个出现次数最多的元素 默认最从多到少排序的,所以看起来就像是截取了前几个元素 89 >>> c = collections.Counter(‘asdfasdfasdfasdfaaaacccsdfefgasdfasdfgasdfasdgf‘) 90 >>> c 91 Counter({‘a‘: 12, ‘f‘: 10, ‘d‘: 9, ‘s‘: 9, ‘c‘: 3, ‘g‘: 3, ‘e‘: 1}) 92 >>> c.most_common(3) 93 [(‘a‘, 12), (‘f‘, 10), (‘d‘, 9)] 94 >>> 95 """ 96 ‘‘‘List the n most common elements and their counts from the most 97 common to the least. If n is None, then list all element counts. 98 99 >>> Counter(‘abcdeabcdabcaba‘).most_common(3)100 [(‘a‘, 5), (‘b‘, 4), (‘c‘, 3)]101 102 ‘‘‘103 # Emulate Bag.sortedByCount from Smalltalk104 if n is None:105 return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)106 return _heapq.nlargest(n, self.iteritems(), key=_itemgetter(1))107 108 def elements(self):109 """ 计数器中的所有元素,注:此处非所有元素集合,而是包含所有元素集合的迭代器 110 >>> c = collections.Counter(‘aaaabbbcc‘)111 >>> c112 Counter({‘a‘: 4, ‘b‘: 3, ‘c‘: 2})113 #因为是迭代器,所以要用循环取出114 >>> c.elements()115 <itertools.chain object at 0x7fb491a27890>116 >>> for ele in c.elements():print ele;117 ... 118 a119 a120 a121 a122 c123 c124 b125 b126 b127 >>> 128 """129 ‘‘‘Iterator over elements repeating each as many times as its count.130 131 >>> c = Counter(‘ABCABC‘)132 >>> sorted(c.elements())133 [‘A‘, ‘A‘, ‘B‘, ‘B‘, ‘C‘, ‘C‘]134 135 # Knuth‘s example for prime factors of 1836: 2**2 * 3**3 * 17**1136 >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})137 >>> product = 1138 >>> for factor in prime_factors.elements(): # loop over factors139 ... product *= factor # and multiply them140 >>> product141 142 Note, if an element‘s count has been set to zero or is a negative143 number, elements() will ignore it.144 145 ‘‘‘146 # Emulate Bag.do from Smalltalk and Multiset.begin from C++.147 return _chain.from_iterable(_starmap(_repeat, self.iteritems()))148 149 # Override dict methods where necessary150 151 @classmethod152 def fromkeys(cls, iterable, v=None):153 # There is no equivalent method for counters because setting v=1154 # means that no element can have a count greater than one.155 raise NotImplementedError(156 ‘Counter.fromkeys() is undefined. Use Counter(iterable) instead.‘)157 158 def update(self, iterable=None, **kwds):159 """ 更新计数器,其实就是增加;如果原来没有,则新建,如果有则加一 160 >>> c161 Counter({‘a‘: 4, ‘b‘: 3, ‘c‘: 2})162 >>> c.update(‘a‘)163 >>> c164 Counter({‘a‘: 5, ‘b‘: 3, ‘c‘: 2})165 >>> d = collections.Counter(‘ccddee‘)166 >>> c.update(d)167 >>> c168 Counter({‘a‘: 5, ‘c‘: 4, ‘b‘: 3, ‘e‘: 2, ‘d‘: 2})169 >>> 170 """171 ‘‘‘Like dict.update() but add counts instead of replacing them.172 173 Source can be an iterable, a dictionary, or another Counter instance.174 175 >>> c = Counter(‘which‘)176 >>> c.update(‘witch‘) # add elements from another iterable177 >>> d = Counter(‘watch‘)178 >>> c.update(d) # add elements from another counter179 >>> c[‘h‘] # four ‘h‘ in which, witch, and watch180 181 ‘‘‘182 # The regular dict.update() operation makes no sense here because the183 # replace behavior results in the some of original untouched counts184 # being mixed-in with all of the other counts for a mismash that185 # doesn‘t have a straight-forward interpretation in most counting186 # contexts. Instead, we implement straight-addition. Both the inputs187 # and outputs are allowed to contain zero and negative counts.188 189 if iterable is not None:190 if isinstance(iterable, Mapping):191 if self:192 self_get = self.get193 for elem, count in iterable.iteritems():194 self[elem] = self_get(elem, 0) + count195 else:196 super(Counter, self).update(iterable) # fast path when counter is empty197 else:198 self_get = self.get199 for elem in iterable:200 self[elem] = self_get(elem, 0) + 1201 if kwds:202 self.update(kwds)203 204 def subtract(self, iterable=None, **kwds):205 """ 相减,原来的计数器中的每一个元素的数量减去后添加的元素的数量 206 >>> c = collections.Counter(‘aabbcc‘)207 >>> c208 Counter({‘a‘: 2, ‘c‘: 2, ‘b‘: 2})209 >>> c.subtract(‘a‘)210 >>> c211 Counter({‘c‘: 2, ‘b‘: 2, ‘a‘: 1})212 >>> 213 """214 ‘‘‘Like dict.update() but subtracts counts instead of replacing them.215 Counts can be reduced below zero. Both the inputs and outputs are216 allowed to contain zero and negative counts.217 218 Source can be an iterable, a dictionary, or another Counter instance.219 220 >>> c = Counter(‘which‘)221 >>> c.subtract(‘witch‘) # subtract elements from another iterable222 >>> c.subtract(Counter(‘watch‘)) # subtract elements from another counter223 >>> c[‘h‘] # 2 in which, minus 1 in witch, minus 1 in watch224 >>> c[‘w‘] # 1 in which, minus 1 in witch, minus 1 in watch225 -1226 227 ‘‘‘228 if iterable is not None:229 self_get = self.get230 if isinstance(iterable, Mapping):231 for elem, count in iterable.items():232 self[elem] = self_get(elem, 0) - count233 else:234 for elem in iterable:235 self[elem] = self_get(elem, 0) - 1236 if kwds:237 self.subtract(kwds)238 239 def copy(self):240 """ 拷贝 """241 ‘Return a shallow copy.‘242 return self.__class__(self)243 244 def __reduce__(self):245 """ 返回一个元组(类型,元组) """246 return self.__class__, (dict(self),)247 248 def __delitem__(self, elem):249 """ 删除元素 """250 ‘Like dict.__delitem__() but does not raise KeyError for missing values.‘251 if elem in self:252 super(Counter, self).__delitem__(elem)253 254 def __repr__(self):255 if not self:256 return ‘%s()‘ % self.__class__.__name__257 items = ‘, ‘.join(map(‘%r: %r‘.__mod__, self.most_common()))258 return ‘%s({%s})‘ % (self.__class__.__name__, items)259 260 # Multiset-style mathematical operations discussed in:261 # Knuth TAOCP Volume II section 4.6.3 exercise 19262 # and at http://en.wikipedia.org/wiki/Multiset263 #264 # Outputs guaranteed to only include positive counts.265 #266 # To strip negative and zero counts, add-in an empty counter:267 # c += Counter()268 269 def __add__(self, other):270 ‘‘‘Add counts from two counters.271 272 >>> Counter(‘abbb‘) + Counter(‘bcc‘)273 Counter({‘b‘: 4, ‘c‘: 2, ‘a‘: 1})274 275 ‘‘‘276 if not isinstance(other, Counter):277 return NotImplemented278 result = Counter()279 for elem, count in self.items():280 newcount = count + other[elem]281 if newcount > 0:282 result[elem] = newcount283 for elem, count in other.items():284 if elem not in self and count > 0:285 result[elem] = count286 return result287 288 def __sub__(self, other):289 ‘‘‘ Subtract count, but keep only results with positive counts.290 291 >>> Counter(‘abbbc‘) - Counter(‘bccd‘)292 Counter({‘b‘: 2, ‘a‘: 1})293 294 ‘‘‘295 if not isinstance(other, Counter):296 return NotImplemented297 result = Counter()298 for elem, count in self.items():299 newcount = count - other[elem]300 if newcount > 0:301 result[elem] = newcount302 for elem, count in other.items():303 if elem not in self and count < 0:304 result[elem] = 0 - count305 return result306 307 def __or__(self, other):308 ‘‘‘Union is the maximum of value in either of the input counters.309 310 >>> Counter(‘abbb‘) | Counter(‘bcc‘)311 Counter({‘b‘: 3, ‘c‘: 2, ‘a‘: 1})312 313 ‘‘‘314 if not isinstance(other, Counter):315 return NotImplemented316 result = Counter()317 for elem, count in self.items():318 other_count = other[elem]319 newcount = other_count if count < other_count else count320 if newcount > 0:321 result[elem] = newcount322 for elem, count in other.items():323 if elem not in self and count > 0:324 result[elem] = count325 return result326 327 def __and__(self, other):328 ‘‘‘ Intersection is the minimum of corresponding counts.329 330 >>> Counter(‘abbb‘) & Counter(‘bcc‘)331 Counter({‘b‘: 1})332 333 ‘‘‘334 if not isinstance(other, Counter):335 return NotImplemented336 result = Counter()337 for elem, count in self.items():338 other_count = other[elem]339 newcount = count if count < other_count else other_count340 if newcount > 0:341 result[elem] = newcount342 return resultcollections.Counter
也可以传入列表或元组
1 >>> li = [11,22,33,44,11,22,22]2 >>> c1 = collections.Counter(li)3 >>> c14 Counter({22: 3, 11: 2, 33: 1, 44: 1})5 >>>
2、有序字典(orderedDict)
orderedDict是对字典类型的补充,他记住了字典元素添加的顺序
有序字典和原生字典的使用完全一样
唯一的区别,orderedDict在内部做了一个排序,它其实是在内部维护一个列表,因为列表是有序的
1 class OrderedDict(dict): 2 ‘Dictionary that remembers insertion order‘ 3 # An inherited dict maps keys to values. 4 # The inherited dict provides __getitem__, __len__, __contains__, and get. 5 # The remaining methods are order-aware. 6 # Big-O running times for all methods are the same as regular dictionaries. 7 8 # The internal self.__map dict maps keys to links in a doubly linked list. 9 # The circular doubly linked list starts and ends with a sentinel element. 10 # The sentinel element never gets deleted (this simplifies the algorithm). 11 # Each link is stored as a list of length three: [PREV, NEXT, KEY]. 12 13 def __init__(self, *args, **kwds): 14 ‘‘‘Initialize an ordered dictionary. The signature is the same as 15 regular dictionaries, but keyword arguments are not recommended because 16 their insertion order is arbitrary. 17 18 ‘‘‘ 19 if len(args) > 1: 20 raise TypeError(‘expected at most 1 arguments, got %d‘ % len(args)) 21 try: 22 self.__root 23 except AttributeError: 24 self.__root = root = [] # sentinel node 25 root[:] = [root, root, None] 26 self.__map = {} 27 self.__update(*args, **kwds) 28 29 def __setitem__(self, key, value, dict_setitem=dict.__setitem__): 30 ‘od.__setitem__(i, y) <==> od[i]=y‘ 31 # Setting a new item creates a new link at the end of the linked list, 32 # and the inherited dictionary is updated with the new key/value pair. 33 if key not in self: 34 root = self.__root 35 last = root[0] 36 last[1] = root[0] = self.__map[key] = [last, root, key] 37 return dict_setitem(self, key, value) 38 39 def __delitem__(self, key, dict_delitem=dict.__delitem__): 40 ‘od.__delitem__(y) <==> del od[y]‘ 41 # Deleting an existing item uses self.__map to find the link which gets 42 # removed by updating the links in the predecessor and successor nodes. 43 dict_delitem(self, key) 44 link_prev, link_next, _ = self.__map.pop(key) 45 link_prev[1] = link_next # update link_prev[NEXT] 46 link_next[0] = link_prev # update link_next[PREV] 47 48 def __iter__(self): 49 ‘od.__iter__() <==> iter(od)‘ 50 # Traverse the linked list in order. 51 root = self.__root 52 curr = root[1] # start at the first node 53 while curr is not root: 54 yield curr[2] # yield the curr[KEY] 55 curr = curr[1] # move to next node 56 57 def __reversed__(self): 58 ‘od.__reversed__() <==> reversed(od)‘ 59 # Traverse the linked list in reverse order. 60 root = self.__root 61 curr = root[0] # start at the last node 62 while curr is not root: 63 yield curr[2] # yield the curr[KEY] 64 curr = curr[0] # move to previous node 65 66 def clear(self): 67 ‘od.clear() -> None. Remove all items from od.‘ 68 root = self.__root 69 root[:] = [root, root, None] 70 self.__map.clear() 71 dict.clear(self) 72 73 # -- the following methods do not depend on the internal structure -- 74 75 def keys(self): 76 ‘od.keys() -> list of keys in od‘ 77 return list(self) 78 79 def values(self): 80 ‘od.values() -> list of values in od‘ 81 return [self[key] for key in self] 82 83 def items(self): 84 ‘od.items() -> list of (key, value) pairs in od‘ 85 return [(key, self[key]) for key in self] 86 87 def iterkeys(self): 88 ‘od.iterkeys() -> an iterator over the keys in od‘ 89 return iter(self) 90 91 def itervalues(self): 92 ‘od.itervalues -> an iterator over the values in od‘ 93 for k in self: 94 yield self[k] 95 96 def iteritems(self): 97 ‘od.iteritems -> an iterator over the (key, value) pairs in od‘ 98 for k in self: 99 yield (k, self[k])100 101 update = MutableMapping.update102 103 __update = update # let subclasses override update without breaking __init__104 105 __marker = object()106 107 def pop(self, key, default=__marker):108 ‘‘‘od.pop(k[,d]) -> v, remove specified key and return the corresponding109 value. If key is not found, d is returned if given, otherwise KeyError110 is raised.111 112 ‘‘‘113 if key in self:114 result = self[key]115 del self[key]116 return result117 if default is self.__marker:118 raise KeyError(key)119 return default120 121 def setdefault(self, key, default=None):122 ‘od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od‘123 if key in self:124 return self[key]125 self[key] = default126 return default127 128 def popitem(self, last=True):129 ‘‘‘od.popitem() -> (k, v), return and remove a (key, value) pair.130 Pairs are returned in LIFO order if last is true or FIFO order if false.131 132 ‘‘‘133 if not self:134 raise KeyError(‘dictionary is empty‘)135 key = next(reversed(self) if last else iter(self))136 value = self.pop(key)137 return key, value138 139 def __repr__(self, _repr_running={}):140 ‘od.__repr__() <==> repr(od)‘141 call_key = id(self), _get_ident()142 if call_key in _repr_running:143 return ‘...‘144 _repr_running[call_key] = 1145 try:146 if not self:147 return ‘%s()‘ % (self.__class__.__name__,)148 return ‘%s(%r)‘ % (self.__class__.__name__, self.items())149 finally:150 del _repr_running[call_key]151 152 def __reduce__(self):153 ‘Return state information for pickling‘154 items = [[k, self[k]] for k in self]155 inst_dict = vars(self).copy()156 for k in vars(OrderedDict()):157 inst_dict.pop(k, None)158 if inst_dict:159 return (self.__class__, (items,), inst_dict)160 return self.__class__, (items,)161 162 def copy(self):163 ‘od.copy() -> a shallow copy of od‘164 return self.__class__(self)165 166 @classmethod167 def fromkeys(cls, iterable, value=None):168 ‘‘‘OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.169 If not specified, the value defaults to None.170 171 ‘‘‘172 self = cls()173 for key in iterable:174 self[key] = value175 return self176 177 def __eq__(self, other):178 ‘‘‘od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive179 while comparison to a regular mapping is order-insensitive.180 181 ‘‘‘182 if isinstance(other, OrderedDict):183 return dict.__eq__(self, other) and all(_imap(_eq, self, other))184 return dict.__eq__(self, other)185 186 def __ne__(self, other):187 ‘od.__ne__(y) <==> od!=y‘188 return not self == other189 190 # -- the following methods support python 3.x style dictionary views --191 192 def viewkeys(self):193 "od.viewkeys() -> a set-like object providing a view on od‘s keys"194 return KeysView(self)195 196 def viewvalues(self):197 "od.viewvalues() -> an object providing a view on od‘s values"198 return ValuesView(self)199 200 def viewitems(self):201 "od.viewitems() -> a set-like object providing a view on od‘s items"202 return ItemsView(self)203 204 OrderedDictorderedDict
3、默认字典(defaultdict)
学习需求:
‘‘‘将一个数字列表分割成字典,‘k1‘的值为小于66的值,‘k2‘的值为大于66的值li = [11,22,33,44,55,66,77,88,99,90]‘‘‘
1 li = [11,22,33,44,55,66,77,88,99,90] 2 dic = {} 3 for ele in li: 4 if ele < 66: 5 if ‘k1‘ in dic.keys(): 6 dic[‘k1‘].append(ele) 7 else: 8 #创建只有一个元素的列表 9 dic[‘k1‘] = [ele,]10 else:11 if ‘k2‘ in dic.keys():12 dic[‘k2‘].append(ele);13 else:14 dic[‘k2‘] = [ele,]原生字典
1 from collections import Counter,defaultdict;2 li = [11,22,33,44,55,66,77,88,99,90,87]3 my_dic = defaultdict(list)4 for ele in li:5 if ele > 66:6 my_dic[‘k2‘].append(ele)7 else:8 my_dic[‘k1‘].append(ele)默认字典
defaultdict是对字典类型的补充,他默认给字典的值设置了一个类型,字典默认的是None,可以给字典设置一个默认类型,比如列表,元组等
1 class defaultdict(dict): 2 """ 3 defaultdict(default_factory[, ...]) --> dict with default factory 4 5 The default factory is called without arguments to produce 6 a new value when a key is not present, in __getitem__ only. 7 A defaultdict compares equal to a dict with the same items. 8 All remaining arguments are treated the same as if they were 9 passed to the dict constructor, including keyword arguments.10 """11 def copy(self): # real signature unknown; restored from __doc__12 """ D.copy() -> a shallow copy of D. """13 pass14 15 def __copy__(self, *args, **kwargs): # real signature unknown16 """ D.copy() -> a shallow copy of D. """17 pass18 19 def __getattribute__(self, name): # real signature unknown; restored from __doc__20 """ x.__getattribute__(‘name‘) <==> x.name """21 pass22 23 def __init__(self, default_factory=None, **kwargs): # known case of _collections.defaultdict.__init__24 """25 defaultdict(default_factory[, ...]) --> dict with default factory26 27 The default factory is called without arguments to produce28 a new value when a key is not present, in __getitem__ only.29 A defaultdict compares equal to a dict with the same items.30 All remaining arguments are treated the same as if they were31 passed to the dict constructor, including keyword arguments.32 33 # (copied from class doc)34 """35 pass36 37 def __missing__(self, key): # real signature unknown; restored from __doc__38 """39 __missing__(key) # Called by __getitem__ for missing key; pseudo-code:40 if self.default_factory is None: raise KeyError((key,))41 self[key] = value = self.default_factory()42 return value43 """44 pass45 46 def __reduce__(self, *args, **kwargs): # real signature unknown47 """ Return state information for pickling. """48 pass49 50 def __repr__(self): # real signature unknown; restored from __doc__51 """ x.__repr__() <==> repr(x) """52 pass53 54 default_factory = property(lambda self: object(), lambda self, v: None, lambda self: None) # default55 """Factory for default value called by __missing__()."""56 57 defaultdictdefaultdict
4、可命名元组(namedtuple)
创建一个扩展tuple的类,Mytuple——类名
1 #通过可命名元组创建一个类 2 >>> Mytuple = collections.namedtuple(‘Mytuple‘,[‘x‘,‘y‘]) 3 #通过这个类创建对象 4 >>> new_tuple = Mytuple(1,2) 5 >>> new_tuple 6 Mytuple(x=1, y=2) 7 #可以通过名称来调用 8 >>> new_tuple.x 9 110 >>> new_tuple.y11 212 >>> old_tuple = ([1,2]) 13 >>> old_tuple14 [1, 2]15 >>>
1 class Mytuple(__builtin__.tuple) 2 | Mytuple(x, y) 3 | 4 | Method resolution order: 5 | Mytuple 6 | __builtin__.tuple 7 | __builtin__.object 8 | 9 | Methods defined here: 10 | 11 | __getnewargs__(self) 12 | Return self as a plain tuple. Used by copy and pickle. 13 | 14 | __getstate__(self) 15 | Exclude the OrderedDict from pickling 16 | 17 | __repr__(self) 18 | Return a nicely formatted representation string 19 | 20 | _asdict(self) 21 | Return a new OrderedDict which maps field names to their values 22 | 23 | _replace(_self, **kwds) 24 | Return a new Mytuple object replacing specified fields with new values 25 | 26 | ---------------------------------------------------------------------- 27 | Class methods defined here: 28 | 29 | _make(cls, iterable, new=<built-in method __new__ of type object>, len=<built-in function len>) from __builtin__.type 30 | Make a new Mytuple object from a sequence or iterable 31 | 32 | ---------------------------------------------------------------------- 33 | Static methods defined here: 34 | 35 | __new__(_cls, x, y) 36 | Create new instance of Mytuple(x, y) 37 | 38 | ---------------------------------------------------------------------- 39 | Data descriptors defined here: 40 | 41 | __dict__ 42 | Return a new OrderedDict which maps field names to their values 43 | 44 | x 45 | Alias for field number 0 46 | 47 | y 48 | Alias for field number 1 49 | 50 | ---------------------------------------------------------------------- 51 | Data and other attributes defined here: 52 | 53 | _fields = (‘x‘, ‘y‘) 54 | 55 | ---------------------------------------------------------------------- 56 | Methods inherited from __builtin__.tuple: 57 | 58 | __add__(...) 59 | x.__add__(y) <==> x+y 60 | 61 | __contains__(...) 62 | x.__contains__(y) <==> y in x 63 | 64 | __eq__(...) 65 | x.__eq__(y) <==> x==y 66 | 67 | __ge__(...) 68 | x.__ge__(y) <==> x>=y 69 | 70 | __getattribute__(...) 71 | x.__getattribute__(‘name‘) <==> x.name 72 | 73 | __getitem__(...) 74 | x.__getitem__(y) <==> x[y] 75 | 76 | __getslice__(...) 77 | x.__getslice__(i, j) <==> x[i:j] 78 | 79 | Use of negative indices is not supported. 80 | 81 | __gt__(...) 82 | x.__gt__(y) <==> x>y 83 | 84 | __hash__(...) 85 | x.__hash__() <==> hash(x) 86 | 87 | __iter__(...) 88 | x.__iter__() <==> iter(x) 89 | 90 | __le__(...) 91 | x.__le__(y) <==> x<=y 92 | 93 | __len__(...) 94 | x.__len__() <==> len(x) 95 | 96 | __lt__(...) 97 | x.__lt__(y) <==> x<y 98 | 99 | __mul__(...)100 | x.__mul__(n) <==> x*n101 | 102 | __ne__(...)103 | x.__ne__(y) <==> x!=y104 | 105 | __rmul__(...)106 | x.__rmul__(n) <==> n*x107 | 108 | __sizeof__(...)109 | T.__sizeof__() -- size of T in memory, in bytes110 | 111 | count(...)112 | T.count(value) -> integer -- return number of occurrences of value113 | 114 | index(...)115 | T.index(value, [start, [stop]]) -> integer -- return first index of value.116 | Raises ValueError if the value is not present.Mytuple class
5、双向队列(deque)
两边都可以取,两边都可以插入,这就有一个线程安全的问题,谁先得到就会有一个线程锁,防止抢占资源
一个线程安全的双向队列
1 class deque(object): 2 """ 3 deque([iterable[, maxlen]]) --> deque object 4 5 Build an ordered collection with optimized access from its endpoints. 6 """ 7 def append(self, *args, **kwargs): # real signature unknown 8 """ 追加元素到队列 9 >>> q = collections.deque() 10 >>> q.append(1) 11 >>> q.append(11) 12 >>> q.append(111) 13 >>> q.append(1111) 14 >>> q 15 deque([1, 11, 111, 1111]) 16 >>> 17 """ 18 """ Add an element to the right side of the deque. """ 19 pass 20 21 def appendleft(self, *args, **kwargs): # real signature unknown 22 """ 加入元素到队列的左边 23 >>> q 24 deque([1, 11, 111]) 25 >>> q.appendleft(2) 26 >>> q 27 deque([2, 1, 11, 111]) 28 >>> 29 """ 30 """ Add an element to the left side of the deque. """ 31 pass 32 33 def clear(self, *args, **kwargs): # real signature unknown 34 """ 清除队列中所有元素 35 >>> q 36 deque([2, 2, 1, 11, 111]) 37 >>> q.clear() 38 >>> q 39 deque([]) 40 >>> 41 """ 42 """ Remove all elements from the deque. """ 43 pass 44 45 def count(self, value): # real signature unknown; restored from __doc__ 46 """ 计算元素的个数 47 >>> q 48 deque([2, 1, 11, 111]) 49 >>> q.count(2) 50 1 51 >>> q.appendleft(2) 52 >>> q.count(2) 53 2 54 >>> q 55 deque([2, 2, 1, 11, 111]) 56 >>> 57 """ 58 """ D.count(value) -> integer -- return number of occurrences of value """ 59 return 0 60 61 def extend(self, *args, **kwargs): # real signature unknown 62 """ 扩展队列从一个迭代器中 63 >>> q 64 deque([]) 65 >>> li = [11,22,33,44,55] 66 >>> q.extend(li) 67 >>> q 68 deque([11, 22, 33, 44, 55]) 69 >>> 70 """ 71 """ Extend the right side of the deque with elements from the iterable """ 72 pass 73 74 def extendleft(self, *args, **kwargs): # real signature unknown 75 """ 从队列左边扩展元素 76 >>> q 77 deque([11, 22, 33, 44, 55]) 78 >>> lileft = (1,2,3,4,5) 79 >>> q.extendleft(lileft) 80 >>> q 81 deque([5, 4, 3, 2, 1, 11, 22, 33, 44, 55]) 82 >>> 83 """ 84 """ Extend the left side of the deque with elements from the iterable """ 85 pass 86 87 def pop(self, *args, **kwargs): # real signature unknown 88 """移除队列最后一个元素并取得 89 >>> q = collections.deque() 90 >>> q.append(1) 91 >>> q.append(11) 92 >>> q.append(111) 93 >>> q.append(1111) 94 >>> q 95 deque([1, 11, 111, 1111]) 96 >>> a = q.pop() 97 >>> a 98 1111 99 >>> q100 deque([1, 11, 111])101 >>> 102 """103 """ Remove and return the rightmost element. """104 pass105 106 def popleft(self, *args, **kwargs): # real signature unknown107 """ 从队列左边移除一个元素并取得108 >>> q109 deque([5, 4, 3, 2, 1, 11, 22, 33, 44, 55])110 >>> b = q.popleft()111 >>> b112 5113 >>> 114 """115 """ Remove and return the leftmost element. """116 pass117 118 def remove(self, value): # real signature unknown; restored from __doc__119 """ 移除第一个被找到的值,如果没有,会抛出异常120 >>> q121 deque([4, 3, 2, 1, 11, 22, 33, 44, 55])122 >>> q.remove(5)123 Traceback (most recent call last):124 File "<stdin>", line 1, in <module>125 ValueError: deque.remove(x): x not in deque126 >>> q.remove(4)127 >>> q128 deque([3, 2, 1, 11, 22, 33, 44, 55])129 >>> 130 """131 """ D.remove(value) -- remove first occurrence of value. """132 pass133 134 def reverse(self): # real signature unknown; restored from __doc__135 """ 反转队列136 >>> q137 deque([3, 2, 1, 11, 22, 33, 44, 55])138 >>> q.reverse()139 >>> q140 deque([55, 44, 33, 22, 11, 1, 2, 3])141 >>> 142 """143 """ D.reverse() -- reverse *IN PLACE* """144 pass145 146 def rotate(self, *args, **kwargs): # real signature unknown147 """ 从右边移动值到左边,默认移动一个148 >>> q149 deque([55, 44, 33, 22, 11, 1, 2, 3])150 >>> q.rotate()151 >>> q152 deque([3, 55, 44, 33, 22, 11, 1, 2])153 >>> q.rotate(2)154 >>> q155 deque([1, 2, 3, 55, 44, 33, 22, 11])156 >>> q.rotate(2)157 >>> q158 deque([22, 11, 1, 2, 3, 55, 44, 33])159 >>> 160 """161 """ Rotate the deque n steps to the right (default n=1). If n is negative, rotates left. """162 pass163 164 def __copy__(self, *args, **kwargs): # real signature unknown165 """ Return a shallow copy of a deque. """166 pass167 168 def __delitem__(self, y): # real signature unknown; restored from __doc__169 """ x.__delitem__(y) <==> del x[y] """170 pass171 172 def __eq__(self, y): # real signature unknown; restored from __doc__173 """ x.__eq__(y) <==> x==y """174 pass175 176 def __getattribute__(self, name): # real signature unknown; restored from __doc__177 """ x.__getattribute__(‘name‘) <==> x.name """178 pass179 180 def __getitem__(self, y): # real signature unknown; restored from __doc__181 """ x.__getitem__(y) <==> x[y] """182 pass183 184 def __ge__(self, y): # real signature unknown; restored from __doc__185 """ x.__ge__(y) <==> x>=y """186 pass187 188 def __gt__(self, y): # real signature unknown; restored from __doc__189 """ x.__gt__(y) <==> x>y """190 pass191 192 def __iadd__(self, y): # real signature unknown; restored from __doc__193 """ x.__iadd__(y) <==> x+=y """194 pass195 196 def __init__(self, iterable=(), maxlen=None): # known case of _collections.deque.__init__197 """198 deque([iterable[, maxlen]]) --> deque object199 200 Build an ordered collection with optimized access from its endpoints.201 # (copied from class doc)202 """203 pass204 205 def __iter__(self): # real signature unknown; restored from __doc__206 """ x.__iter__() <==> iter(x) """207 pass208 209 def __len__(self): # real signature unknown; restored from __doc__210 """ x.__len__() <==> len(x) """211 pass212 213 def __le__(self, y): # real signature unknown; restored from __doc__214 """ x.__le__(y) <==> x<=y """215 pass216 217 def __lt__(self, y): # real signature unknown; restored from __doc__218 """ x.__lt__(y) <==> x<y """219 pass220 221 @staticmethod # known case of __new__222 def __new__(S, *more): # real signature unknown; restored from __doc__223 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """224 pass225 226 def __ne__(self, y): # real signature unknown; restored from __doc__227 """ x.__ne__(y) <==> x!=y """228 pass229 230 def __reduce__(self, *args, **kwargs): # real signature unknown231 """ Return state information for pickling. """232 pass233 234 def __repr__(self): # real signature unknown; restored from __doc__235 """ x.__repr__() <==> repr(x) """236 pass237 238 def __reversed__(self): # real signature unknown; restored from __doc__239 """ D.__reversed__() -- return a reverse iterator over the deque """240 pass241 242 def __setitem__(self, i, y): # real signature unknown; restored from __doc__243 """ x.__setitem__(i, y) <==> x[i]=y """244 pass245 246 def __sizeof__(self): # real signature unknown; restored from __doc__247 """ D.__sizeof__() -- size of D in memory, in bytes """248 pass249 250 maxlen = property(lambda self: object(), lambda self, v: None, lambda self: None) # default251 """maximum size of a deque or None if unbounded"""252 253 254 __hash__ = Nonedeque
6、单向队列(Queue.Queue)——先进先出(FIFO(First In First Out))
队列:先进先出 FIFO
栈:先进后出 弹夹
1 #创建一个最多十个内容的队列 2 >>> q = Queue.Queue(10) 3 >>> q 4 <Queue.Queue instance at 0x7f82028390e0> 5 #给队列放值,用put 6 >>> q.put(1) 7 >>> q.put(2) 8 >>> q.put(3) 9 #取值用get,get取值是阻塞的10 >>> q.get()11 112 >>> q.get()13 214 >>> q.get()15 316 #当取完后,再取就会阻塞住,直到队列中有值才继续17 >>> q.get()
1 class Queue: 2 """Create a queue object with a given maximum size. 3 4 If maxsize is <= 0, the queue size is infinite. 5 """ 6 def __init__(self, maxsize=0): 7 self.maxsize = maxsize 8 self._init(maxsize) 9 # mutex must be held whenever the queue is mutating. All methods 10 # that acquire mutex must release it before returning. mutex 11 # is shared between the three conditions, so acquiring and 12 # releasing the conditions also acquires and releases mutex. 13 self.mutex = _threading.Lock() 14 # Notify not_empty whenever an item is added to the queue; a 15 # thread waiting to get is notified then. 16 self.not_empty = _threading.Condition(self.mutex) 17 # Notify not_full whenever an item is removed from the queue; 18 # a thread waiting to put is notified then. 19 self.not_full = _threading.Condition(self.mutex) 20 # Notify all_tasks_done whenever the number of unfinished tasks 21 # drops to zero; thread waiting to join() is notified to resume 22 self.all_tasks_done = _threading.Condition(self.mutex) 23 self.unfinished_tasks = 0 24 25 def task_done(self): 26 """Indicate that a formerly enqueued task is complete. 27 28 Used by Queue consumer threads. For each get() used to fetch a task, 29 a subsequent call to task_done() tells the queue that the processing 30 on the task is complete. 31 32 If a join() is currently blocking, it will resume when all items 33 have been processed (meaning that a task_done() call was received 34 for every item that had been put() into the queue). 35 36 Raises a ValueError if called more times than there were items 37 placed in the queue. 38 """ 39 self.all_tasks_done.acquire() 40 try: 41 unfinished = self.unfinished_tasks - 1 42 if unfinished <= 0: 43 if unfinished < 0: 44 raise ValueError(‘task_done() called too many times‘) 45 self.all_tasks_done.notify_all() 46 self.unfinished_tasks = unfinished 47 finally: 48 self.all_tasks_done.release() 49 50 def join(self): 51 """Blocks until all items in the Queue have been gotten and processed. 52 53 The count of unfinished tasks goes up whenever an item is added to the 54 queue. The count goes down whenever a consumer thread calls task_done() 55 to indicate the item was retrieved and all work on it is complete. 56 57 When the count of unfinished tasks drops to zero, join() unblocks. 58 """ 59 self.all_tasks_done.acquire() 60 try: 61 while self.unfinished_tasks: 62 self.all_tasks_done.wait() 63 finally: 64 self.all_tasks_done.release() 65 66 def qsize(self): 67 """Return the approximate size of the queue (not reliable!).""" 68 self.mutex.acquire() 69 n = self._qsize() 70 self.mutex.release() 71 return n 72 73 def empty(self): 74 """Return True if the queue is empty, False otherwise (not reliable!).""" 75 self.mutex.acquire() 76 n = not self._qsize() 77 self.mutex.release() 78 return n 79 80 def full(self): 81 """Return True if the queue is full, False otherwise (not reliable!).""" 82 self.mutex.acquire() 83 n = 0 < self.maxsize == self._qsize() 84 self.mutex.release() 85 return n 86 87 def put(self, item, block=True, timeout=None): 88 """Put an item into the queue. 89 90 If optional args ‘block‘ is true and ‘timeout‘ is None (the default), 91 block if necessary until a free slot is available. If ‘timeout‘ is 92 a non-negative number, it blocks at most ‘timeout‘ seconds and raises 93 the Full exception if no free slot was available within that time. 94 Otherwise (‘block‘ is false), put an item on the queue if a free slot 95 is immediately available, else raise the Full exception (‘timeout‘ 96 is ignored in that case). 97 """ 98 self.not_full.acquire() 99 try:100 if self.maxsize > 0:101 if not block:102 if self._qsize() == self.maxsize:103 raise Full104 elif timeout is None:105 while self._qsize() == self.maxsize:106 self.not_full.wait()107 elif timeout < 0:108 raise ValueError("‘timeout‘ must be a non-negative number")109 else:110 endtime = _time() + timeout111 while self._qsize() == self.maxsize:112 remaining = endtime - _time()113 if remaining <= 0.0:114 raise Full115 self.not_full.wait(remaining)116 self._put(item)117 self.unfinished_tasks += 1118 self.not_empty.notify()119 finally:120 self.not_full.release()121 122 def put_nowait(self, item):123 """Put an item into the queue without blocking.124 125 Only enqueue the item if a free slot is immediately available.126 Otherwise raise the Full exception.127 """128 return self.put(item, False)129 130 def get(self, block=True, timeout=None):131 """Remove and return an item from the queue.132 133 If optional args ‘block‘ is true and ‘timeout‘ is None (the default),134 block if necessary until an item is available. If ‘timeout‘ is135 a non-negative number, it blocks at most ‘timeout‘ seconds and raises136 the Empty exception if no item was available within that time.137 Otherwise (‘block‘ is false), return an item if one is immediately138 available, else raise the Empty exception (‘timeout‘ is ignored139 in that case).140 """141 self.not_empty.acquire()142 try:143 if not block:144 if not self._qsize():145 raise Empty146 elif timeout is None:147 while not self._qsize():148 self.not_empty.wait()149 elif timeout < 0:150 raise ValueError("‘timeout‘ must be a non-negative number")151 else:152 endtime = _time() + timeout153 while not self._qsize():154 remaining = endtime - _time()155 if remaining <= 0.0:156 raise Empty157 self.not_empty.wait(remaining)158 item = self._get()159 self.not_full.notify()160 return item161 finally:162 self.not_empty.release()163 164 def get_nowait(self):165 """Remove and return an item from the queue without blocking.166 167 Only get an item if one is immediately available. Otherwise168 raise the Empty exception.169 """170 return self.get(False)171 172 # Override these methods to implement other queue organizations173 # (e.g. stack or priority queue).174 # These will only be called with appropriate locks held175 176 # Initialize the queue representation177 def _init(self, maxsize):178 self.queue = deque()179 180 def _qsize(self, len=len):181 return len(self.queue)182 183 # Put a new item in the queue184 def _put(self, item):185 self.queue.append(item)186 187 # Get an item from the queue188 def _get(self):189 return self.queue.popleft()Queue.Queue
双向队列和单向队列的区别
双向队列是两边都可以插入数据和读取数据,单向队列只能从一边取和插
双向队列是在collections模块中,单向队列是在Queue模块中
都是线程安全的
会通过help方法找到collections,大致知道计算器、有序字典、默认字典、可命名元组和双向队列
迭代器和生成器
一、迭代器
对于python列表的for循环,他的内部原理:查看下一个元素是否存在,如果存在,则取出,如果不存在,则报异常StopIteration。(python内部对异常已处理)
1 >>> c2 Counter({‘d‘: 4, ‘s‘: 4, ‘a‘: 2, ‘c‘: 2, ‘b‘: 2, ‘e‘: 2, ‘f‘: 2, ‘g‘: 1})3 >>> c.elements()4 <itertools.chain object at 0x7f4424b59890>5 >>>
迭代器的内容要用循环取出来
1 class listiterator(object) 2 | Methods defined here: 3 | 4 | __getattribute__(...) 5 | x.__getattribute__(‘name‘) <==> x.name 6 | 7 | __iter__(...) 8 | x.__iter__() <==> iter(x) 9 | 10 | __length_hint__(...)11 | Private method returning an estimate of len(list(it)).12 | 13 | next(...)14 | x.next() -> the next value, or raise StopIterationlistiterator
二、生成器
range不是生成器 xrange是生成器
redlines不是生成器 xreadlines是生成器
1 >>> print range(10)2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]3 >>> print xrange(10)4 xrange(10)5 >>>
xrange(10)创建时只是一个对象,只有在循环或使用时才会在内存中开辟空间
生成器内部基于yield创建,即:对于生成器只有使用时才创建,从而避免内存浪费
练习:有如下列表: [13, 22, 6, 99, 11]利用列表的下标进行循环>>> li[13, 22, 6, 99, 11]>>> i = 0>>> while i < len(li):... print li[i]... i += 1... 132269911
练习:有如下列表: [13, 22, 6, 99, 11] 请按照一下规则计算:13 和 22 比较,将大的值放在右侧,即:[13, 22, 6, 99, 11]22 和 6 比较,将大的值放在右侧,即:[13, 6, 22, 99, 11]22 和 99 比较,将大的值放在右侧,即:[13, 6, 22, 99, 11]99 和 42 比较,将大的值放在右侧,即:[13, 6, 22, 11, 99,] 13 和 6 比较,将大的值放在右侧,即:[6, 13, 22, 11, 99,]...
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 li = [12, 33, 6, 99, 11] 4 ‘‘‘ 5 方法一:第一种方法是固定一个数a与后面的数b相比,如果a大于b,就把a\b对调,再用a和下面的一个数比,全部比完后会得到一个最小的数放在最左面,然后用第二个数继续往下比,以此类推就得到了从小到大的排序 6 ‘‘‘ 7 for i in range(len(li) - 1): 8 for j in range(i+1,len(li)): 9 if li[i] > li[j]:10 temp = li[i];11 li[i] = li[j]12 li[j] = temp13 14 print li15 16 ‘‘‘17 方法二:两个相邻的数比较,如果左边的数比右边的大,就调换位置,一个循环后会把最大的数放在最右边,以此类推,所有数字都循环完后会得到从小到大的排序18 ‘‘‘19 for x in range(len(li)):20 for i in range(len(li)-1):21 if li[i] > li[i+1]:22 t = li[i];23 li[i] = li[i+1];24 li[i+1] = t25 print li;冒泡排序
函数
函数分为内置函数、自定义函数、导入函数。函数就是按功能划分的代码块
一、内置函数
内置函数就是python为用户提供的快捷方法
先看几个常用的
help()函数可以得到方法的一些帮助
1 >>> help(list) 2 Help on class list in module __builtin__: 3 4 class list(object) 5 | list() -> new empty list 6 | list(iterable) -> new list initialized from iterable‘s items 7 | 8 | Methods defined here: 9 | 10 | __add__(...)11 | x.__add__(y) <==> x+y12 | 13 | __contains__(...)14 | x.__contains__(y) <==> y in x15 | 16 | __delitem__(...)17 | x.__delitem__(y) <==> del x[y]18 | 19 | __delslice__(...)20 | x.__delslice__(i, j) <==> del x[i:j]21 | 22 | Use of negative indices is not supported.help(list)
dir()可以把一个类的方法全部打印出来
1 >>> dir(list)2 [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__delslice__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getslice__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__setslice__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]3 >>>dir(list)
vars()可以显示出所有的变量
1 >>> vars()2 {‘a‘: xrange(10), ‘c‘: Counter({‘d‘: 4, ‘s‘: 4, ‘a‘: 2, ‘c‘: 2, ‘b‘: 2, ‘e‘: 2, ‘f‘: 2, ‘g‘: 1}), ‘__builtins__‘: <module ‘__builtin__‘ (built-in)>, ‘__package__‘: None, ‘i‘: 1, ‘collections‘: <module ‘collections‘ from ‘/usr/lib/python2.7/collections.pyc‘>, ‘tab‘: <module ‘tab‘ from ‘/usr/lib/python2.7/dist-packages/tab.pyc‘>, ‘__name__‘: ‘__main__‘, ‘li‘: [13, 22, 6, 99, 11], ‘__doc__‘: None}3 >>>vars()
type()查看变量的类型
>>> type(li)<type ‘list‘>>>>
abc()取绝对值
>>> abs(-9)9>>> abs(0)0>>> abs(33)33>>>
divmod()输入两个数字,得到一个元组(商、余数)
>>> divmod(10,3)(3, 1)>>> divmod(10,2)(5, 0)>>>
ord()输入一个字符,返回ASCII
>>> ord(‘a‘)97>>>
chr()输入ASCII,返回字符
>>> chr(100)‘d‘>>>
cmp()比较两个数大小,大于返回1,小于返回-1,等于返回0
>>> cmp(20,2)1>>> cmp(20,20)0>>> cmp(20,21)-1>>>
eval()可以计算一个字符串的值
>>> eval(‘200/4+4‘)54>>>
format()格式化输出字符串
>>> a = ‘I am a {0},hello {1}‘>>> print a.format(‘techer‘,‘world‘)I am a techer,hello world>>>
hex(x)转换成十六进制
>>> hex(255)‘0xff‘>>>
id()返回对象的内存地址
>>> a = 20>>> id(a)22802320>>>
input()输入内容
1 #input接收到的是输入的是什么就是什么类型,比如输入22,那么a类型就是数值类型 2 >>> a = input() 3 22 4 >>> type(a) 5 <type ‘int‘> 6 #输入一个hello,接收到的就是一个变量名,根据下面的异常提示也可以知道 7 >>> a = input() 8 hello 9 Traceback (most recent call last):10 File "<stdin>", line 1, in <module>11 File "<string>", line 1, in <module>12 NameError: name ‘hello‘ is not defined13 #如果想得到一个字符串,输入的时候要加引号14 >>> a = input()15 ‘hello‘16 >>> type(a)17 <type ‘str‘>18 >>>input
int()输入一个数字,转换成整型,字符串不能是小数形式的
>>> a = ‘22‘>>> type(a)<type ‘str‘>>>> b = int(a)>>> type(b)<type ‘int‘>>>> b22>>>
len()计算长度
>>> li [13, 22, 6, 99, 11]>>> len(li)5>>> s = ‘hello world‘>>> len(s)11>>>
max()找出最大值
>>> a = 22>>> b = 33>>> c = 21>>> max(a,b,c)33>>>
min()找出最小值
>>> a,b,c(22, 33, 21)>>> min(a,b,c)21>>> t = (11,111,1111)>>> min(t)11>>>
oct()转换成八进制
>>> a = 100>>> oct(a)‘0144‘>>>
pow()求幂
>>> pow(2,2) 4>>> pow(2,10) 1024>>>
range()生成一个连续的数字序列
>>> range(5)[0, 1, 2, 3, 4]>>> range(2,8)[2, 3, 4, 5, 6, 7]>>>
raw_input()接收终端输入的内容
>>> a = raw_input()hello>>> a‘hello‘>>> a = raw_input()33>>> a‘33‘>>>
reload(module)重新导入模块
如果一个模块已经导入,修改过这个模块后需要重新导入,就需要用reload重新导入
sum()计算一个数字序列的和
>>> sum([1,2,3])6>>>
all()如果里面有空值,就返回False
>>> a = [1,2,3,‘‘]>>> all(a)False>>> a = [1,2,3,‘a‘]>>> all(a) True>>>
any()只要有一个是真就是真
>>> a = []>>> any(a)False>>> a = [1,2,‘‘]>>> any(a) True>>>
enumerate()循环序列,前面加一个序号,默认从0开始,可以指定起始值
>>> a = [11,22,33,44,55]>>> for k,v in enumerate(a,1):... print k,v... 1 112 223 334 445 55>>>
二、自定义函数
一、在学习函数之前,一直遵循:面向过程编程,即:根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复制到现需要功能处。
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 # ######### 定义函数 ######### 2 3 # name 叫做函数func的形式参数,简称:形参4 def func(name):5 print name6 7 # ######### 执行函数 ######### 8 # ‘wangwei‘ 叫做函数func的实际参数,简称:实参9 func(‘wangwei‘)普通参数
1 def func(name, age = 18): 2 3 print "%s:%s" %(name,age) 4 5 # 指定参数 6 func(‘wupeiqi‘, 19) 7 # 使用默认参数 8 func(‘alex‘) 9 10 注:默认参数需要放在参数列表最后默认参数动态参数一
1 def func(**kwargs): 2 3 print args 4 5 6 # 执行方式一 7 func(name=‘wupeiqi‘,age=18) 8 9 # 执行方式二10 li = {‘name‘:‘wupeiqi‘, age:18, ‘gender‘:‘male‘}11 func(**li)12 #传入字典时前面要加两个星动态参数二
1 def func(*args, **kwargs):2 3 print args4 print kwargs动态参数三
1 >>> def func(*args,**kwargs): 2 ... print args 3 ... print kwargs 4 ... 5 >>> func(11,22,33) 6 (11, 22, 33) 7 {} 8 >>> func(k1=123,k2=321) 9 ()10 {‘k2‘: 321, ‘k1‘: 123}11 >>> func(1,2,3,4,k1=123,k2=321)12 (1, 2, 3, 4)13 {‘k2‘: 321, ‘k1‘: 123}14 >>> func([1,2,3],k1=123,k2=321)15 ([1, 2, 3],)16 {‘k2‘: 321, ‘k1‘: 123}17 >>>动态函数补充
文件操作
操作文件时,一般需要经历如下步骤:
打开文件操作文件一、打开文件
文件句柄 = file(‘文件路径‘,‘模式‘)
注:python中打开文件有两种方式,即:open(...)和file(...),本质上前者在内部会调用后者来进行文件操作,推荐使用open
打开文件时,需要指定文件路径和以何种方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作。
打开文件的模式有:
r,只读模式(默认)w,只写模式。——不可读,不存在则创建,存在则删除内容。a,追加模式。——可读,不存在则创建,存在则只追加内容。“+”表示可以同时读写某个文件。“+”只有在r+有意思,w+和a+和w、a的效果是一样的
r+,可读写文件——可读,可写,可追加w+,写读a+,同a“U”表示在读取时,可以将\r \n \r\n自动转换成\n(与r或r+模式同时使用)
rUr+U“b”表示处理二进制文件(如:FTP发送上传ISO去镜像文件,linux可忽略,windows处理二进制文件时需标注)
rbwbab二、操作文件
1 class file(object): 2 3 def close(self): # real signature unknown; restored from __doc__ 4 #关闭文件 5 """ 6 close() -> None or (perhaps) an integer. Close the file. 7 8 Sets data attribute .closed to True. A closed file cannot be used for 9 further I/O operations. close() may be called more than once without 10 error. Some kinds of file objects (for example, opened by popen()) 11 may return an exit status upon closing. 12 """ 13 14 def fileno(self): # real signature unknown; restored from __doc__ 15 #文件描述符 16 """ 17 fileno() -> integer "file descriptor". 18 19 This is needed for lower-level file interfaces, such os.read(). 20 """ 21 return 0 22 23 def flush(self): # real signature unknown; restored from __doc__ 24 #刷新文件内部缓冲区 25 """ flush() -> None. Flush the internal I/O buffer. """ 26 pass 27 28 29 def isatty(self): # real signature unknown; restored from __doc__ 30 #判断文件是否是同意tty设备 31 """ isatty() -> true or false. True if the file is connected to a tty device. """ 32 return False 33 34 35 def next(self): # real signature unknown; restored from __doc__ 36 #获取下一行数据,不存在,则报错 37 """ x.next() -> the next value, or raise StopIteration """ 38 pass 39 40 def read(self, size=None): # real signature unknown; restored from __doc__ 41 #读取指定字节数据 42 """ 43 read([size]) -> read at most size bytes, returned as a string. 44 45 If the size argument is negative or omitted, read until EOF is reached. 46 Notice that when in non-blocking mode, less data than what was requested 47 may be returned, even if no size parameter was given. 48 """ 49 pass 50 51 def readinto(self): # real signature unknown; restored from __doc__ 52 #读取到缓冲区,不要用,将被遗弃 53 """ readinto() -> Undocumented. Don‘t use this; it may go away. """ 54 pass 55 56 def readline(self, size=None): # real signature unknown; restored from __doc__ 57 #仅读取一行数据 58 """ 59 readline([size]) -> next line from the file, as a string. 60 61 Retain newline. A non-negative size argument limits the maximum 62 number of bytes to return (an incomplete line may be returned then). 63 Return an empty string at EOF. 64 """ 65 pass 66 67 def readlines(self, size=None): # real signature unknown; restored from __doc__ 68 #读取所有数据,并根据换行保存值列表 69 """ 70 readlines([size]) -> list of strings, each a line from the file. 71 72 Call readline() repeatedly and return a list of the lines so read. 73 The optional size argument, if given, is an approximate bound on the 74 total number of bytes in the lines returned. 75 """ 76 return [] 77 78 def seek(self, offset, whence=None): # real signature unknown; restored from __doc__ 79 #指定文件中指针位置 80 """ 81 seek(offset[, whence]) -> None. Move to new file position. 82 83 Argument offset is a byte count. Optional argument whence defaults to 84 0 (offset from start of file, offset should be >= 0); other values are 1 85 (move relative to current position, positive or negative), and 2 (move 86 relative to end of file, usually negative, although many platforms allow 87 seeking beyond the end of a file). If the file is opened in text mode, 88 only offsets returned by tell() are legal. Use of other offsets causes 89 undefined behavior. 90 Note that not all file objects are seekable. 91 """ 92 pass 93 94 def tell(self): # real signature unknown; restored from __doc__ 95 #获取当前指针位置 96 """ tell() -> current file position, an integer (may be a long integer). """ 97 pass 98 99 def truncate(self, size=None): # real signature unknown; restored from __doc__100 #截断数据,仅保留指定之前数据101 """102 truncate([size]) -> None. Truncate the file to at most size bytes.103 104 Size defaults to the current file position, as returned by tell().105 """106 pass107 108 def write(self, p_str): # real signature unknown; restored from __doc__109 #写内容110 """111 write(str) -> None. Write string str to file.112 113 Note that due to buffering, flush() or close() may be needed before114 the file on disk reflects the data written.115 """116 pass117 118 def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__119 #将一个字符串列表写入文件120 """121 writelines(sequence_of_strings) -> None. Write the strings to the file.122 123 Note that newlines are not added. The sequence can be any iterable object124 producing strings. This is equivalent to calling write() for each string.125 """126 pass127 128 def xreadlines(self): # real signature unknown; restored from __doc__129 #可用于逐行读取文件,非全部130 """131 将弃用,可以用下面的方法132 for line in f: 133 print line.strip();134 """135 """136 xreadlines() -> returns self.137 138 For backward compatibility. File objects now include the performance139 optimizations previously implemented in the xreadlines module.140 """141 passfile
三、with
为了避免打开文件后忘记关闭,可以通过管理上下文,即:
with open(‘log‘,‘r‘) as f: ......
如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。
在python2.7后,with又支持同时对多个文件的上下文进行管理,即:
with open(‘log1‘) as obj1,open(‘log2‘) as obj2: pass
Python学习之路——day03
相关内容
- 暂无相关文章
评论关闭