Python学习第三天

Set集合:

不允许有重复的元素。正如Hash表。创建一个Set的对象: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
View Code

(1)  add()添加一个元素 clear():清空元素

s1=set()
s1.add('alex')
print(s1)

{'alex'}

(2)  difference():

s2=set([‘alex’,’eric’,’tony’])
s3=s2.difference([‘alex’,’eric’])
print(s3)

{'tony'}

(3)difference_update():删除当前set中的所有包含在参数集合里的元素

s2=(['alex','eric','tony'])
print(s2)
s4=s2.difference_update([‘alex’,’eric’])#删除当前set中的所有包含在参数集合里的元素
print(s2)
print(s4)

{'alex', 'tony', 'eric'}
{'tony'}
None

(4)intersection():取交集,新创建一个set。    isdisjoint():如果没有交集,返回true。    issubset():是否是子集。

(5)pop()移除

s2=(['alex','eric','tony'])
print(s2)
ret=s2.pop()
print(ret)

{'alex','eric'}

(6)remove():只去拿不会获取,没有返回值

s2=(['alex','eric','tony'])
print(s2)
ret=s2.remove('tony')
print(ret)

{'alex','eric'}

(7)intersection():交集,(两个字典里都有的话)可能不动也可能更新(要更新的数据)。symmetric_difference():1 差集,(原来的有,新的没有)要删除;2 差集,(原来的没有,新的有)要增加。

# 数据库中原有
old_dict = {
    "#1":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 },
    "#2":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 },
    "#3":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }
}

# cmdb 新汇报的数据
new_dict = {
    "#1":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 800 },
    "#3":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 },
    "#4":{ 'hostname':'c2', 'cpu_count': 2, 'mem_capicity': 80 }
}
old=set(old_dict.keys())
new=set(new_dict.keys())
updateset=old.intersection(new)
delete_set=old.symmetric_difference(updateset)
add_set=new.symmetric_difference(updateset)
print(updateset)
print(delete_set)
print(add_set)

{'#1', '#3'}
{'#2'}
{'#4'}

collection系列

一、计数器(counter)

Counter是对字典类型的补充,用于追踪值的出现次数。

具备字典的所有功能和自己的功能:

具备的功能:

  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
View Code

(1)Counter():分别计算各个字母出现的次数  elements():取出Counter中的所有Key值,items():显示所有的Keys和Values值。

 1 import collections#导入
 2 obj=collections.Counter('aabgoomabbblilifffgg')#分别计算各个字母出现的次数
 3 print(obj)
 4 ret=obj.most_common(4)#排在前4位的
 5 print(ret)
 6 for k in obj.elements():
 7     print(k)
 8 for k,v in obj.items():
 9     print(k,v)
10 
11 Counter({'b': 4, 'g': 3, 'a': 3, 'f': 3, 'i': 2, 'l': 2, 'o': 2, 'm': 1})
12 [('b', 4), ('g', 3), ('a', 3), ('f', 3)]
13 i
14 i
15 g
16 g
17 g
18 b
19 b
20 b
21 b
22 l
23 l
24 o
25 o
26 a
27 a
28 a
29 m
30 f
31 f
32 f
33 i 2
34 g 3
35 b 4
36 l 2
37 o 2
38 a 3
39 m 1
40 f 3

(3)update():新增元素;subtract():删掉元素

 1 import collections#导入
 2 obj=collections.Counter(['11','22','22','33'])
 3 print(obj)
 4 obj.update(['eric','11','11'])
 5 print(obj)
 6 obj.subtract(['eric','11','11'])
 7 print(obj)
 8 
 9 Counter({'22': 2, '33': 1, '11': 1})
10 Counter({'11': 3, '22': 2, 'eric': 1, '33': 1})
11 Counter({'22': 2, '33': 1, '11': 1, 'eric': 0})

二、有序字典

orderdDict是对字典类型的补充,他记住了字典元素添加的顺序

class OrderedDict(dict):
    'Dictionary that remembers insertion order'
    # An inherited dict maps keys to values.
    # The inherited dict provides __getitem__, __len__, __contains__, and get.
    # The remaining methods are order-aware.
    # Big-O running times for all methods are the same as regular dictionaries.

    # The internal self.__map dict maps keys to links in a doubly linked list.
    # The circular doubly linked list starts and ends with a sentinel element.
    # The sentinel element never gets deleted (this simplifies the algorithm).
    # Each link is stored as a list of length three:  [PREV, NEXT, KEY].

    def __init__(self, *args, **kwds):
        '''Initialize an ordered dictionary.  The signature is the same as
        regular dictionaries, but keyword arguments are not recommended because
        their insertion order is arbitrary.

        '''
        if len(args) > 1:
            raise TypeError('expected at most 1 arguments, got %d' % len(args))
        try:
            self.__root
        except AttributeError:
            self.__root = root = []                     # sentinel node
            root[:] = [root, root, None]
            self.__map = {}
        self.__update(*args, **kwds)

    def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
        'od.__setitem__(i, y) <==> od[i]=y'
        # Setting a new item creates a new link at the end of the linked list,
        # and the inherited dictionary is updated with the new key/value pair.
        if key not in self:
            root = self.__root
            last = root[0]
            last[1] = root[0] = self.__map[key] = [last, root, key]
        return dict_setitem(self, key, value)

    def __delitem__(self, key, dict_delitem=dict.__delitem__):
        'od.__delitem__(y) <==> del od[y]'
        # Deleting an existing item uses self.__map to find the link which gets
        # removed by updating the links in the predecessor and successor nodes.
        dict_delitem(self, key)
        link_prev, link_next, _ = self.__map.pop(key)
        link_prev[1] = link_next                        # update link_prev[NEXT]
        link_next[0] = link_prev                        # update link_next[PREV]

    def __iter__(self):
        'od.__iter__() <==> iter(od)'
        # Traverse the linked list in order.
        root = self.__root
        curr = root[1]                                  # start at the first node
        while curr is not root:
            yield curr[2]                               # yield the curr[KEY]
            curr = curr[1]                              # move to next node

    def __reversed__(self):
        'od.__reversed__() <==> reversed(od)'
        # Traverse the linked list in reverse order.
        root = self.__root
        curr = root[0]                                  # start at the last node
        while curr is not root:
            yield curr[2]                               # yield the curr[KEY]
            curr = curr[0]                              # move to previous node

    def clear(self):
        'od.clear() -> None.  Remove all items from od.'
        root = self.__root
        root[:] = [root, root, None]
        self.__map.clear()
        dict.clear(self)

    # -- the following methods do not depend on the internal structure --

    def keys(self):
        'od.keys() -> list of keys in od'
        return list(self)

    def values(self):
        'od.values() -> list of values in od'
        return [self[key] for key in self]

    def items(self):
        'od.items() -> list of (key, value) pairs in od'
        return [(key, self[key]) for key in self]

    def iterkeys(self):
        'od.iterkeys() -> an iterator over the keys in od'
        return iter(self)

    def itervalues(self):
        'od.itervalues -> an iterator over the values in od'
        for k in self:
            yield self[k]

    def iteritems(self):
        'od.iteritems -> an iterator over the (key, value) pairs in od'
        for k in self:
            yield (k, self[k])

    update = MutableMapping.update

    __update = update # let subclasses override update without breaking __init__

    __marker = object()

    def pop(self, key, default=__marker):
        '''od.pop(k[,d]) -> v, remove specified key and return the corresponding
        value.  If key is not found, d is returned if given, otherwise KeyError
        is raised.

        '''
        if key in self:
            result = self[key]
            del self[key]
            return result
        if default is self.__marker:
            raise KeyError(key)
        return default

    def setdefault(self, key, default=None):
        'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
        if key in self:
            return self[key]
        self[key] = default
        return default

    def popitem(self, last=True):
        '''od.popitem() -> (k, v), return and remove a (key, value) pair.
        Pairs are returned in LIFO order if last is true or FIFO order if false.

        '''
        if not self:
            raise KeyError('dictionary is empty')
        key = next(reversed(self) if last else iter(self))
        value = self.pop(key)
        return key, value

    def __repr__(self, _repr_running={}):
        'od.__repr__() <==> repr(od)'
        call_key = id(self), _get_ident()
        if call_key in _repr_running:
            return '...'
        _repr_running[call_key] = 1
        try:
            if not self:
                return '%s()' % (self.__class__.__name__,)
            return '%s(%r)' % (self.__class__.__name__, self.items())
        finally:
            del _repr_running[call_key]

    def __reduce__(self):
        'Return state information for pickling'
        items = [[k, self[k]] for k in self]
        inst_dict = vars(self).copy()
        for k in vars(OrderedDict()):
            inst_dict.pop(k, None)
        if inst_dict:
            return (self.__class__, (items,), inst_dict)
        return self.__class__, (items,)

    def copy(self):
        'od.copy() -> a shallow copy of od'
        return self.__class__(self)

    @classmethod
    def fromkeys(cls, iterable, value=None):
        '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
        If not specified, the value defaults to None.

        '''
        self = cls()
        for key in iterable:
            self[key] = value
        return self

    def __eq__(self, other):
        '''od.__eq__(y) <==> od==y.  Comparison to another OD is order-sensitive
        while comparison to a regular mapping is order-insensitive.

        '''
        if isinstance(other, OrderedDict):
            return dict.__eq__(self, other) and all(_imap(_eq, self, other))
        return dict.__eq__(self, other)

    def __ne__(self, other):
        'od.__ne__(y) <==> od!=y'
        return not self == other

    # -- the following methods support python 3.x style dictionary views --

    def viewkeys(self):
        "od.viewkeys() -> a set-like object providing a view on od's keys"
        return KeysView(self)

    def viewvalues(self):
        "od.viewvalues() -> an object providing a view on od's values"
        return ValuesView(self)

    def viewitems(self):
        "od.viewitems() -> a set-like object providing a view on od's items"
        return ItemsView(self)

OrderedDict
View Code
1 import collections
2 dic=collections.OrderedDict()
3 dic['k1']='v1'
4 dic['k2']='v2'
5 dic['k3']='v3'
6 print(dic)
7 
8 OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])

(1)move_to_end():移动到最后的位置。

import collections
dic=collections.OrderedDict()
dic['k1']='v1'
dic['k2']='v2'
dic['k3']='v3'
dic.move_to_end('k1')
print(dic)

OrderedDict([('k2', 'v2'), ('k3', 'v3'), ('k1', 'v1')])

(2)popitem():删除最后放进的元素。(后进先出)

 1 import collections
 2 dic=collections.OrderedDict()
 3 dic['k1']='v1'
 4 dic['k2']='v2'
 5 dic['k3']='v3'
 6 print(dic)
 7 dic.popitem()
 8 print(dic)
 9 
10 OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
11 OrderedDict([('k1', 'v1'), ('k2', 'v2')])

(3)update():更新添加

 1 import collections
 2 dic=collections.OrderedDict()
 3 dic['k1']='v1'
 4 dic['k2']='v2'
 5 dic['k3']='v3'
 6 print(dic)
 7 dic.update({'k1':'v111','k10':'v10'})
 8 print(dic)
 9 
10 OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
11 OrderedDict([('k1', 'v111'), ('k2', 'v2'), ('k3', 'v3'), ('k10', 'v10')])

三、默认字典

defaultdict是对字典的类型的补充,他默认给字典的值设置了一个类型。

class defaultdict(dict):
    """
    defaultdict(default_factory[, ...]) --> dict with default factory
    
    The default factory is called without arguments to produce
    a new value when a key is not present, in __getitem__ only.
    A defaultdict compares equal to a dict with the same items.
    All remaining arguments are treated the same as if they were
    passed to the dict constructor, including keyword arguments.
    """
    def copy(self): # real signature unknown; restored from __doc__
        """ D.copy() -> a shallow copy of D. """
        pass

    def __copy__(self, *args, **kwargs): # real signature unknown
        """ D.copy() -> a shallow copy of D. """
        pass

    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __init__(self, default_factory=None, **kwargs): # known case of _collections.defaultdict.__init__
        """
        defaultdict(default_factory[, ...]) --> dict with default factory
        
        The default factory is called without arguments to produce
        a new value when a key is not present, in __getitem__ only.
        A defaultdict compares equal to a dict with the same items.
        All remaining arguments are treated the same as if they were
        passed to the dict constructor, including keyword arguments.
        
        # (copied from class doc)
        """
        pass

    def __missing__(self, key): # real signature unknown; restored from __doc__
        """
        __missing__(key) # Called by __getitem__ for missing key; pseudo-code:
          if self.default_factory is None: raise KeyError((key,))
          self[key] = value = self.default_factory()
          return value
        """
        pass

    def __reduce__(self, *args, **kwargs): # real signature unknown
        """ Return state information for pickling. """
        pass

    def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

    default_factory = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """Factory for default value called by __missing__()."""

defaultdict
View Code

(1)defaultdict():默认设置为字典。

1 dic=collections.defaultdict(list)
2 dic['k1'].append('alex')
3 print(dic)
4 
5 defaultdict(<class 'list'>, {'k1': ['alex']})

练习:

有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。即: {'k1': 大于66 'k2': 小于66}
 1 values = [11, 22, 33,44,55,66,77,88,99,90]
 2 
 3 my_dict = {}
 4 
 5 for value in  values:
 6     if value>66:
 7         if my_dict.has_key('k1'):
 8             my_dict['k1'].append(value)
 9         else:
10             my_dict['k1'] = [value]
11     else:
12         if my_dict.has_key('k2'):
13             my_dict['k2'].append(value)
14         else:
15             my_dict['k2'] = [value]
16 
17 原生字典
View Code
 1 from collections import defaultdict
 2 
 3 values = [11, 22, 33,44,55,66,77,88,99,90]
 4 
 5 my_dict = defaultdict(list)
 6 
 7 for value in  values:
 8     if value>66:
 9         my_dict['k1'].append(value)
10     else:
11         my_dict['k2'].append(value)
12 
13 defaultdict字典解决方法
14 
15 默认字典
View Code

四、可命名元组

根据nametuple可以创建一个包含tuple所有功能以及其他功能的类型。

 1 import collections
 2 mytupleclass=collections.namedtuple('mytupleclass',['x','y','z'])
 3 obj=mytupleclass(11,22,33)
 4 print(obj.x)
 5 print(obj.y)
 6 print(obj.z)
 7 
 8 11
 9 22
10 33

help(mytupleclass)#看类里头具有的功能

class Mytuple(__builtin__.tuple)
 |  Mytuple(x, y)
 |  
 |  Method resolution order:
 |      Mytuple
 |      __builtin__.tuple
 |      __builtin__.object
 |  
 |  Methods defined here:
 |  
 |  __getnewargs__(self)
 |      Return self as a plain tuple.  Used by copy and pickle.
 |  
 |  __getstate__(self)
 |      Exclude the OrderedDict from pickling
 |  
 |  __repr__(self)
 |      Return a nicely formatted representation string
 |  
 |  _asdict(self)
 |      Return a new OrderedDict which maps field names to their values
 |  
 |  _replace(_self, **kwds)
 |      Return a new Mytuple object replacing specified fields with new values
 |  
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |  
 |  _make(cls, iterable, new=<built-in method __new__ of type object>, len=<built-in function len>) from __builtin__.type
 |      Make a new Mytuple object from a sequence or iterable
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(_cls, x, y)
 |      Create new instance of Mytuple(x, y)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      Return a new OrderedDict which maps field names to their values
 |  
 |  x
 |      Alias for field number 0
 |  
 |  y
 |      Alias for field number 1
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  _fields = ('x', 'y')
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from __builtin__.tuple:
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |  
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x
 |  
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y
 |  
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __getslice__(...)
 |      x.__getslice__(i, j) <==> x[i:j]
 |      
 |      Use of negative indices is not supported.
 |  
 |  __gt__(...)
 |      x.__gt__(y) <==> x>y
 |  
 |  __hash__(...)
 |      x.__hash__() <==> hash(x)
 |  
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 |  
 |  __le__(...)
 |      x.__le__(y) <==> x<=y
 |  
 |  __len__(...)
 |      x.__len__() <==> len(x)
 |  
 |  __lt__(...)
 |      x.__lt__(y) <==> x<y
 |  
 |  __mul__(...)
 |      x.__mul__(n) <==> x*n
 |  
 |  __ne__(...)
 |      x.__ne__(y) <==> x!=y
 |  
 |  __rmul__(...)
 |      x.__rmul__(n) <==> n*x
 |  
 |  __sizeof__(...)
 |      T.__sizeof__() -- size of T in memory, in bytes
 |  
 |  count(...)
 |      T.count(value) -> integer -- return number of occurrences of value
 |  
 |  index(...)
 |      T.index(value, [start, [stop]]) -> integer -- return first index of value.
 |      Raises ValueError if the value is not present.

Mytuple
View Code

五、双向队列

双向队列

class deque(object):
    """
    deque([iterable[, maxlen]]) --> deque object
    
    Build an ordered collection with optimized access from its endpoints.
    """
    def append(self, *args, **kwargs): # real signature unknown
        """ Add an element to the right side of the deque. """
        pass

    def appendleft(self, *args, **kwargs): # real signature unknown
        """ Add an element to the left side of the deque. """
        pass

    def clear(self, *args, **kwargs): # real signature unknown
        """ Remove all elements from the deque. """
        pass

    def count(self, value): # real signature unknown; restored from __doc__
        """ D.count(value) -> integer -- return number of occurrences of value """
        return 0

    def extend(self, *args, **kwargs): # real signature unknown
        """ Extend the right side of the deque with elements from the iterable """
        pass

    def extendleft(self, *args, **kwargs): # real signature unknown
        """ Extend the left side of the deque with elements from the iterable """
        pass

    def pop(self, *args, **kwargs): # real signature unknown
        """ Remove and return the rightmost element. """
        pass

    def popleft(self, *args, **kwargs): # real signature unknown
        """ Remove and return the leftmost element. """
        pass

    def remove(self, value): # real signature unknown; restored from __doc__
        """ D.remove(value) -- remove first occurrence of value. """
        pass

    def reverse(self): # real signature unknown; restored from __doc__
        """ D.reverse() -- reverse *IN PLACE* """
        pass

    def rotate(self, *args, **kwargs): # real signature unknown
        """ Rotate the deque n steps to the right (default n=1).  If n is negative, rotates left. """
        pass

    def __copy__(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of a deque. """
        pass

    def __delitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__delitem__(y) <==> del x[y] """
        pass

    def __eq__(self, y): # real signature unknown; restored from __doc__
        """ x.__eq__(y) <==> x==y """
        pass

    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __ge__(self, y): # real signature unknown; restored from __doc__
        """ x.__ge__(y) <==> x>=y """
        pass

    def __gt__(self, y): # real signature unknown; restored from __doc__
        """ x.__gt__(y) <==> x>y """
        pass

    def __iadd__(self, y): # real signature unknown; restored from __doc__
        """ x.__iadd__(y) <==> x+=y """
        pass

    def __init__(self, iterable=(), maxlen=None): # known case of _collections.deque.__init__
        """
        deque([iterable[, maxlen]]) --> deque object
        
        Build an ordered collection with optimized access from its endpoints.
        # (copied from class doc)
        """
        pass

    def __iter__(self): # real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass

    def __len__(self): # real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass

    def __le__(self, y): # real signature unknown; restored from __doc__
        """ x.__le__(y) <==> x<=y """
        pass

    def __lt__(self, y): # real signature unknown; restored from __doc__
        """ x.__lt__(y) <==> x<y """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __ne__(self, y): # real signature unknown; restored from __doc__
        """ x.__ne__(y) <==> x!=y """
        pass

    def __reduce__(self, *args, **kwargs): # real signature unknown
        """ Return state information for pickling. """
        pass

    def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

    def __reversed__(self): # real signature unknown; restored from __doc__
        """ D.__reversed__() -- return a reverse iterator over the deque """
        pass

    def __setitem__(self, i, y): # real signature unknown; restored from __doc__
        """ x.__setitem__(i, y) <==> x[i]=y """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ D.__sizeof__() -- size of D in memory, in bytes """
        pass

    maxlen = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """maximum size of a deque or None if unbounded"""


    __hash__ = None

deque

deque
View Code

单项队列(先进先出 FIFO )

class Queue:
    """Create a queue object with a given maximum size.

    If maxsize is <= 0, the queue size is infinite.
    """
    def __init__(self, maxsize=0):
        self.maxsize = maxsize
        self._init(maxsize)
        # mutex must be held whenever the queue is mutating.  All methods
        # that acquire mutex must release it before returning.  mutex
        # is shared between the three conditions, so acquiring and
        # releasing the conditions also acquires and releases mutex.
        self.mutex = _threading.Lock()
        # Notify not_empty whenever an item is added to the queue; a
        # thread waiting to get is notified then.
        self.not_empty = _threading.Condition(self.mutex)
        # Notify not_full whenever an item is removed from the queue;
        # a thread waiting to put is notified then.
        self.not_full = _threading.Condition(self.mutex)
        # Notify all_tasks_done whenever the number of unfinished tasks
        # drops to zero; thread waiting to join() is notified to resume
        self.all_tasks_done = _threading.Condition(self.mutex)
        self.unfinished_tasks = 0

    def task_done(self):
        """Indicate that a formerly enqueued task is complete.

        Used by Queue consumer threads.  For each get() used to fetch a task,
        a subsequent call to task_done() tells the queue that the processing
        on the task is complete.

        If a join() is currently blocking, it will resume when all items
        have been processed (meaning that a task_done() call was received
        for every item that had been put() into the queue).

        Raises a ValueError if called more times than there were items
        placed in the queue.
        """
        self.all_tasks_done.acquire()
        try:
            unfinished = self.unfinished_tasks - 1
            if unfinished <= 0:
                if unfinished < 0:
                    raise ValueError('task_done() called too many times')
                self.all_tasks_done.notify_all()
            self.unfinished_tasks = unfinished
        finally:
            self.all_tasks_done.release()

    def join(self):
        """Blocks until all items in the Queue have been gotten and processed.

        The count of unfinished tasks goes up whenever an item is added to the
        queue. The count goes down whenever a consumer thread calls task_done()
        to indicate the item was retrieved and all work on it is complete.

        When the count of unfinished tasks drops to zero, join() unblocks.
        """
        self.all_tasks_done.acquire()
        try:
            while self.unfinished_tasks:
                self.all_tasks_done.wait()
        finally:
            self.all_tasks_done.release()

    def qsize(self):
        """Return the approximate size of the queue (not reliable!)."""
        self.mutex.acquire()
        n = self._qsize()
        self.mutex.release()
        return n

    def empty(self):
        """Return True if the queue is empty, False otherwise (not reliable!)."""
        self.mutex.acquire()
        n = not self._qsize()
        self.mutex.release()
        return n

    def full(self):
        """Return True if the queue is full, False otherwise (not reliable!)."""
        self.mutex.acquire()
        n = 0 < self.maxsize == self._qsize()
        self.mutex.release()
        return n

    def put(self, item, block=True, timeout=None):
        """Put an item into the queue.

        If optional args 'block' is true and 'timeout' is None (the default),
        block if necessary until a free slot is available. If 'timeout' is
        a non-negative number, it blocks at most 'timeout' seconds and raises
        the Full exception if no free slot was available within that time.
        Otherwise ('block' is false), put an item on the queue if a free slot
        is immediately available, else raise the Full exception ('timeout'
        is ignored in that case).
        """
        self.not_full.acquire()
        try:
            if self.maxsize > 0:
                if not block:
                    if self._qsize() == self.maxsize:
                        raise Full
                elif timeout is None:
                    while self._qsize() == self.maxsize:
                        self.not_full.wait()
                elif timeout < 0:
                    raise ValueError("'timeout' must be a non-negative number")
                else:
                    endtime = _time() + timeout
                    while self._qsize() == self.maxsize:
                        remaining = endtime - _time()
                        if remaining <= 0.0:
                            raise Full
                        self.not_full.wait(remaining)
            self._put(item)
            self.unfinished_tasks += 1
            self.not_empty.notify()
        finally:
            self.not_full.release()

    def put_nowait(self, item):
        """Put an item into the queue without blocking.

        Only enqueue the item if a free slot is immediately available.
        Otherwise raise the Full exception.
        """
        return self.put(item, False)

    def get(self, block=True, timeout=None):
        """Remove and return an item from the queue.

        If optional args 'block' is true and 'timeout' is None (the default),
        block if necessary until an item is available. If 'timeout' is
        a non-negative number, it blocks at most 'timeout' seconds and raises
        the Empty exception if no item was available within that time.
        Otherwise ('block' is false), return an item if one is immediately
        available, else raise the Empty exception ('timeout' is ignored
        in that case).
        """
        self.not_empty.acquire()
        try:
            if not block:
                if not self._qsize():
                    raise Empty
            elif timeout is None:
                while not self._qsize():
                    self.not_empty.wait()
            elif timeout < 0:
                raise ValueError("'timeout' must be a non-negative number")
            else:
                endtime = _time() + timeout
                while not self._qsize():
                    remaining = endtime - _time()
                    if remaining <= 0.0:
                        raise Empty
                    self.not_empty.wait(remaining)
            item = self._get()
            self.not_full.notify()
            return item
        finally:
            self.not_empty.release()

    def get_nowait(self):
        """Remove and return an item from the queue without blocking.

        Only get an item if one is immediately available. Otherwise
        raise the Empty exception.
        """
        return self.get(False)

    # Override these methods to implement other queue organizations
    # (e.g. stack or priority queue).
    # These will only be called with appropriate locks held

    # Initialize the queue representation
    def _init(self, maxsize):
        self.queue = deque()

    def _qsize(self, len=len):
        return len(self.queue)

    # Put a new item in the queue
    def _put(self, item):
        self.queue.append(item)

    # Get an item from the queue
    def _get(self):
        return self.queue.popleft()

Queue.Queue
View Code

(1) append():添加元素

 1 d=collections.deque()
 2 d.append('1')
 3 d.appendleft('10')
 4 d.appendleft('l')
 5 print(d)
 6 r=d.count('l')
 7 print(r)
 8 
 9 
10 deque(['l','10','l'])
11 2

(2)size(),get()

1 import queue
2 q=queue.Queue()
3 q.put('123')
4 q.put('789')
5 print(q.qsize())
6 print(q.get())
7 
8 2
9 123

六、深浅拷贝

import copy

copy.copy()#浅拷贝

copy.deepcopy()#深拷贝

对于数字和字符串来说无论是通过赋值还是通过深浅拷贝,使用的内存地址都是一样的。

(1)数字和字符串的赋值、浅拷贝、深拷贝后的地址是一样的:

 1 import copy
 2 n1=123
 3 print(id(n1))
 4 n2=n1
 5 print(id(n2))
 6 n2=copy.copy(n1)
 7 print(id(n2))
 8 n3=copy.deepcopy(n1)
 9 print(id(n3))
10 #地址是一样的:
11 492584624
12 492584624
13 492584624
14 492584624

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

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

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

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

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

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

 1 import copy
 2 dic={
 3 'cpu':[80,],
 4 'mem':[80,],
 5 'disk':[80,]
 6 }
 7 print('before',dic)
 8 new_dic=copy.deepcopy(dic)
 9 new_dic['cpu'][0]=50
10 print(dic)
11 print(new_dic)
12 
13 before {'disk': [80], 'cpu': [80], 'mem': [80]}
14 {'disk': [80], 'cpu': [80], 'mem': [80]}
15 {'mem': [80], 'cpu': [50], 'disk': [80]}

函数:

定义和使用:

1 def 函数名(参数):
2       
3     ...
4     函数体
5     ...
1 def mail():
2     n=123
3     n+=1
4     print(n)
5 mail()
6 f=mail
7 f()
8 124
9 124

函数的定义主要有如下要点:

  • def:表示函数的关键字
  • 函数名:函数的名称,日后根据函数名调用函数
  • 函数体:函数中进行一系列的逻辑计算,如:发送邮件、计算出 [11,22,38,888,2]中的最大数等...
  • 参数:为函数体提供数据
  • 返回值:当函数执行完毕后,可以给调用者返回数据。

以上要点中,比较重要有参数和返回值:

1、返回值

函数是一个功能块,该功能到底执行成功与否,需要通过返回值来告知调用者。

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 from email.utils import formataddr
 4 def mail():
 5     ret=123
 6     try:
 7        msg=MIMEText('邮件内容','plain','utf-8')
 8        msg['from']=formataddr(['郭晶晶','guojingjing8@163.com'])
 9        msg['to']=formataddr({'郭美奂','240430854@qq.com'})
10        msg['subject']="主题"
11        server=smtplib.smtp("smtp.126.com",25)
12      server.login("guojingjing881007@163.com","zhang911007")
13        server.sendmail('guojingjing8@163.com',['240430854@qq.com',],msg.as_string())
14        server.quit()
15     except Exception:
16         ret=456
17     return ret
18 ret=mail()
19 print(ret)
View Code

函数的普通参数:

形式参数和实际参数:(还是以发送邮件为例,这里user是形式参数,具体的邮箱账号是实际参数)

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 from email.utils import formataddr
 4 def mail(user):
 5     ret=True
 6     try:
 7        msg=MIMEText('邮件内容','plain','utf-8')
 8        msg['from']=formataddr(['郭晶晶','guojingjing881007@163.com'])
 9        msg['to']=formataddr({'郭美奂','1353971628@qq.com'})
10        msg['subject']="主题"
11        server=smtplib.smtp("smtp.126.com",25)
12        server.login("guojingjing881007@163.com","zhang881007")
13        server.sendmail('guojingjing881007@163.com',[user,],msg.as_string())
14        server.quit()
15     except Exception:
16         ret=False
17     return ret
18 ret=mail('240420840@qq.com')
19 if ret:
20 print('发送成功')
21 else:
22 print('发送失败')
View Code

函数的默认参数:

 1 def func(name, age = 18):
 2     
 3     print "%s:%s" %(name,age)
 4 
 5 # 指定参数
 6 func('guojingjing', 26)
 7 # 使用默认参数
 8 func('yoyo')
 9 
10 注:默认参数需要放在参数列表最后
11 
12 默认参数

函数的动态参数:

 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)
12 
13 动态参数-序列
1 def show(*arg,**kwargs):
2 print(arg,type(arg))#将传入的参数转换成元组
3 print(kwargs,type(kwargs))#将传入的参数转换成字典
4 show(11,22,33,44,n1=88,alex=’yunwei’)
 1 def func(**kwargs):
 2 
 3     print args
 4 
 5 
 6 # 执行方式一
 7 func(name='yangyun',age=18)
 8 
 9 # 执行方式二
10 li = {'name':'yangyun', age:18, 'gender':'male'}
11 func(**li)
12 
13 动态参数-字典
def show(*arg,**kwargs):
print(arg,type(arg))#将传入的参数转换成元组
print(kwargs,type(kwargs))#将传入的参数转换成字典
l=[11,22,33,44]
d={‘n1’:88,’alex’:’yunwei’}
show(*l,**d)#注意这里的星号

使用动态参数实现字符串格式化:

 1 s1='{0}is{1}'
 2 l=['alex','teacher']
 3 result=s1.format(*l)
 4 print(result)
 5 
 6 s1="{name} is {acter}"
 7 result=s1.format(name='alex',acter='teacher')
 8 print(result)
 9 
10 s1="{name} is {acter}"
11 d={'name':'alex','acter':'teacher'}
12 result=s1.format(**d)
13 print(result)
14 
15 alexisteacher
16 alex is teacher
17 alex is teacher

Python lambda表达式:

学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即:

1 # 普通条件语句
2 if 1 == 1:
3     name = 'teacher'
4 else:
5     name = 'actor'
6    
7 # 三元运算
8 name = 'teacher' if 1 == 1 else 'actor'

对于简单的函数,也存在一种简便的表示方式,即:lambda表达式

func=lambda a:a+1#创建形式参数,函数内容为a+1,并把结果返回
ret=func(99)
print(ret)

内置函数:

(1)ord()和chr()

ord(‘a’)#将字符翻译成数字
97
chr()#将数字翻译成字符

(2)Random.randint(1,99)#随机生成1到99的数值

(3)enumerate()

1 li=['alex','eric','marry']
2 for i,item in enumerate(li,1):
3     print(i,item)
4 1 alex
5 2 eric
6 3 marry

(4)eval(“6*8”)#直接输出乘积

(5)map

遍历序列,对序列中每个元素进行操作,最终获取新的序列

(6)filter

对于序列中的元素进行筛选,最终获取符合条件的序列

1 li = [11, 22, 33]
2 
3 new_list = filter(lambda arg: arg > 22, li)
4 
5 #filter第一个参数为空,将获取原来序列

(7)global()#当前可用的所有的变量    hash()#用来做字典的时候的Key   hex()#将数字转 成16进制  max()#最大  min()#最小值   oct()#8进制  open()#打开文件     用的  pow()#幂    round(8.9)#四舍五入  range()#

1 i=range(0,10)
2 
3 for k in i:print(k)#循环拿到0到9的值

文件操作:

操作文件时,一般需要经历如下步骤:

  • 打开文件
  • 操作文件 

一、打开文件

文件句柄 = file('文件路径', '模式')

注:python中打开文件有两种方式,即:open(...) 和  file(...) ,本质上前者在内部会调用后者来进行文件操作,推荐使用 open

打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作。

打开文件的模式有:

  • r,只读模式(默认)。
  • w,只写模式。【不可读;不存在则创建;存在则删除内容;】
  • a,追加模式。【可读;   不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件

  • r+,可读写文件。【可读;可写;可追加】
  • w+,写读
  • a+,同a

"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)

  • rU
  • r+U

"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

  • rb
  • wb
  • ab

二、文件操作

class file(object):
  
    def close(self): # real signature unknown; restored from __doc__
        关闭文件
        """
        close() -> None or (perhaps) an integer.  Close the file.
         
        Sets data attribute .closed to True.  A closed file cannot be used for
        further I/O operations.  close() may be called more than once without
        error.  Some kinds of file objects (for example, opened by popen())
        may return an exit status upon closing.
        """
 
    def fileno(self): # real signature unknown; restored from __doc__
        文件描述符  
         """
        fileno() -> integer "file descriptor".
         
        This is needed for lower-level file interfaces, such os.read().
        """
        return 0    
 
    def flush(self): # real signature unknown; restored from __doc__
        刷新文件内部缓冲区
        """ flush() -> None.  Flush the internal I/O buffer. """
        pass
 
 
    def isatty(self): # real signature unknown; restored from __doc__
        判断文件是否是同意tty设备
        """ isatty() -> true or false.  True if the file is connected to a tty device. """
        return False
 
 
    def next(self): # real signature unknown; restored from __doc__
        获取下一行数据,不存在,则报错
        """ x.next() -> the next value, or raise StopIteration """
        pass
 
    def read(self, size=None): # real signature unknown; restored from __doc__
        读取指定字节数据
        """
        read([size]) -> read at most size bytes, returned as a string.
         
        If the size argument is negative or omitted, read until EOF is reached.
        Notice that when in non-blocking mode, less data than what was requested
        may be returned, even if no size parameter was given.
        """
        pass
 
    def readinto(self): # real signature unknown; restored from __doc__
        读取到缓冲区,不要用,将被遗弃
        """ readinto() -> Undocumented.  Don't use this; it may go away. """
        pass
 
    def readline(self, size=None): # real signature unknown; restored from __doc__
        仅读取一行数据
        """
        readline([size]) -> next line from the file, as a string.
         
        Retain newline.  A non-negative size argument limits the maximum
        number of bytes to return (an incomplete line may be returned then).
        Return an empty string at EOF.
        """
        pass
 
    def readlines(self, size=None): # real signature unknown; restored from __doc__
        读取所有数据,并根据换行保存值列表
        """
        readlines([size]) -> list of strings, each a line from the file.
         
        Call readline() repeatedly and return a list of the lines so read.
        The optional size argument, if given, is an approximate bound on the
        total number of bytes in the lines returned.
        """
        return []
 
    def seek(self, offset, whence=None): # real signature unknown; restored from __doc__
        指定文件中指针位置
        """
        seek(offset[, whence]) -> None.  Move to new file position.
         
        Argument offset is a byte count.  Optional argument whence defaults to
        0 (offset from start of file, offset should be >= 0); other values are 1
        (move relative to current position, positive or negative), and 2 (move
        relative to end of file, usually negative, although many platforms allow
        seeking beyond the end of a file).  If the file is opened in text mode,
        only offsets returned by tell() are legal.  Use of other offsets causes
        undefined behavior.
        Note that not all file objects are seekable.
        """
        pass
 
    def tell(self): # real signature unknown; restored from __doc__
        获取当前指针位置
        """ tell() -> current file position, an integer (may be a long integer). """
        pass
 
    def truncate(self, size=None): # real signature unknown; restored from __doc__
        截断数据,仅保留指定之前数据
        """
        truncate([size]) -> None.  Truncate the file to at most size bytes.
         
        Size defaults to the current file position, as returned by tell().
        """
        pass
 
    def write(self, p_str): # real signature unknown; restored from __doc__
        写内容
        """
        write(str) -> None.  Write string str to file.
         
        Note that due to buffering, flush() or close() may be needed before
        the file on disk reflects the data written.
        """
        pass
 
    def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__
        将一个字符串列表写入文件
        """
        writelines(sequence_of_strings) -> None.  Write the strings to the file.
         
        Note that newlines are not added.  The sequence can be any iterable object
        producing strings. This is equivalent to calling write() for each string.
        """
        pass
 
    def xreadlines(self): # real signature unknown; restored from __doc__
        可用于逐行读取文件,非全部
        """
        xreadlines() -> returns self.
         
        For backward compatibility. File objects now include the performance
        optimizations previously implemented in the xreadlines module.
        """
        pass
View Code

三、with

为了避免打开文件后忘记关闭,可以通过管理上下文,即:

with open('log','r') as f:
     
    ...

如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:

with open('log1') as obj1, open('log2') as obj2:
    pass

 

 

posted @ 2016-02-03 17:57  Peony_Y  阅读(209)  评论(0编辑  收藏  举报