基本数据类型补充: 


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

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

 1:创建

1 s = set()
2 s = {11,22,33,55}

2:转换

1 li = [11,22,33,44]
2 tu = (11,22,33,44)
3 st = '123'
4 s = set(li)

 3:intersection , intersection_update方法

a = {11,22,33,44}
b = {22,66,77,88}
ret = a.intersection(b)
print(ret)

intersection取得两个集合中的交集元素,并将这些元素以一个新的集合返回给一个变量接收

a = {11,22,33,44}
b = {22,66,77,88}
a.intersection_update(b)
print(a)

intersection_update取得两个集合的交集元素,并更新a集合

4:isdisjoint , issubset , issuperset方法

1 s = {11,22,33,44}
2 b = {11,22,77,55}
3 ret = s.isdisjoint(b)#有交集返回False,没有交集返回True
4 print(ret)
5 ## False

issubset判断是否为子集

a = {11,22,33,44}
b = {11,44}
ret = b.issubset(a)
print(ret)
##########################################
True

issuperset判断是否为父集

a = {11,22,33,44}
b = {11,44}
ret = a.issubset(b)
print(ret)
##########################################
False

5:discard , remove , pop

1 s = {11,22,33,44}
2 s.remove(11)
3 print(s)
4 s.discard(22)
5 print(s)
6 s.pop()
7 print(s)

三者都能达到移除元素的效果,区别在于remove移除集合中不存在的元素时会报错,discard移除不存在的元素是不会报错,pop无法精确控制移除哪个元素,按其自身的规则随机移除元素,返回被移除的元素,可以使用变量接收其返回值

6:symmetric_difference取差集

 1 s = {11,22,33,44}
 2 b = {11,22,77,55}
 3 r1 = s.difference(b)
 4 r2 = b.difference(s)
 5 print(r1)
 6 print(r2)
 7 ret = s.symmetric_difference(b)
 8 print(ret)
 9 ## set([33, 44])
10 ## set([77, 55])
11 ## set([33, 44, 77, 55])

symmetric_difference返回两个集合中不是交集的元素

上面的代码中,将symmetric_difference换成symmetric_difference_update则表示将两个集合中不是交集的部分赋值给s

7:union , update方法

1 s = {11,22,33,44}
2 b = {11,22,77,55}
3 ret = s.union(b)
4 print(ret)
5 ## set([33, 11, 44, 77, 22, 55])

union方法合并两个集合

1 s = {11,22,33,44}
2 b = {11,22,77,55}
3 s.update(b)
4 print(s)
5 ## set([33, 11, 44, 77, 22, 55])

update方法更新s集合,将b集合中的元素添加到s集合中!update方法也可以传递一个列表,如:update([23,45,67])

 练习题:有下面两个字典

要求:

1)两个字典中有相同键的,则将new_dict中的值更新到old_dict对应键的值

2)old_dict中存在的键且new_dict中没有的键,在old_dict中删除,并把new_dict中的键值更新到old_dict中

3)最后输出old_dict

 1 # 数据库中原有
 2 old_dict = {
 3     "#1":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 },
 4     "#2":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 },
 5     "#3":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }
 6 }
 7    
 8 # cmdb 新汇报的数据
 9 new_dict = {
10     "#1":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 800 },
11     "#3":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 },
12     "#4":{ 'hostname':'c2', 'cpu_count': 2, 'mem_capicity': 80 }
13 }
old_keys = set(old_dict.keys())
new_keys = set(new_dict.keys())
#需要更新元素的键
update_keys = old_keys.intersection(new_keys)
print(update_keys)
#需要删除元素的键
del_keys = old_keys.difference(new_keys)
#需要添加元素的键
add_keys = new_keys.difference(old_keys)
print(del_keys)
print(add_keys)
update_keys = list(update_keys)
for i in update_keys :
    old_dict[i] = new_dict[i]
del_keys = list(del_keys)
for j in del_keys :
    del old_dict[j]
for k in list(add_keys) :
    old_dict[k] = new_dict[k]
print(old_dict)
########################################
{'#3': {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80}, '#1': {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 800}, '#4': {'hostname': 'c2', 'cpu_count': 2, 'mem_capicity': 80}}
答案

collections系列

一、计数器(counter)

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

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

c = Counter('abcdeabcdabcaba')
print c
输出:Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
  1 ########################################################################
  2 ###  Counter
  3 ########################################################################
  4 
  5 class Counter(dict):
  6     '''Dict subclass for counting hashable items.  Sometimes called a bag
  7     or multiset.  Elements are stored as dictionary keys and their counts
  8     are stored as dictionary values.
  9 
 10     >>> c = Counter('abcdeabcdabcaba')  # count elements from a string
 11 
 12     >>> c.most_common(3)                # three most common elements
 13     [('a', 5), ('b', 4), ('c', 3)]
 14     >>> sorted(c)                       # list all unique elements
 15     ['a', 'b', 'c', 'd', 'e']
 16     >>> ''.join(sorted(c.elements()))   # list elements with repetitions
 17     'aaaaabbbbcccdde'
 18     >>> sum(c.values())                 # total of all counts
 19 
 20     >>> c['a']                          # count of letter 'a'
 21     >>> for elem in 'shazam':           # update counts from an iterable
 22     ...     c[elem] += 1                # by adding 1 to each element's count
 23     >>> c['a']                          # now there are seven 'a'
 24     >>> del c['b']                      # remove all 'b'
 25     >>> c['b']                          # now there are zero 'b'
 26 
 27     >>> d = Counter('simsalabim')       # make another counter
 28     >>> c.update(d)                     # add in the second counter
 29     >>> c['a']                          # now there are nine 'a'
 30 
 31     >>> c.clear()                       # empty the counter
 32     >>> c
 33     Counter()
 34 
 35     Note:  If a count is set to zero or reduced to zero, it will remain
 36     in the counter until the entry is deleted or the counter is cleared:
 37 
 38     >>> c = Counter('aaabbc')
 39     >>> c['b'] -= 2                     # reduce the count of 'b' by two
 40     >>> c.most_common()                 # 'b' is still in, but its count is zero
 41     [('a', 3), ('c', 1), ('b', 0)]
 42 
 43     '''
 44     # References:
 45     #   http://en.wikipedia.org/wiki/Multiset
 46     #   http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html
 47     #   http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
 48     #   http://code.activestate.com/recipes/259174/
 49     #   Knuth, TAOCP Vol. II section 4.6.3
 50 
 51     def __init__(self, iterable=None, **kwds):
 52         '''Create a new, empty Counter object.  And if given, count elements
 53         from an input iterable.  Or, initialize the count from another mapping
 54         of elements to their counts.
 55 
 56         >>> c = Counter()                           # a new, empty counter
 57         >>> c = Counter('gallahad')                 # a new counter from an iterable
 58         >>> c = Counter({'a': 4, 'b': 2})           # a new counter from a mapping
 59         >>> c = Counter(a=4, b=2)                   # a new counter from keyword args
 60 
 61         '''
 62         super(Counter, self).__init__()
 63         self.update(iterable, **kwds)
 64 
 65     def __missing__(self, key):
 66         """ 对于不存在的元素,返回计数器为0 """
 67         'The count of elements not in the Counter is zero.'
 68         # Needed so that self[missing_item] does not raise KeyError
 69         return 0
 70 
 71     def most_common(self, n=None):
 72         """ 数量大于等n的所有元素和计数器 """
 73         '''List the n most common elements and their counts from the most
 74         common to the least.  If n is None, then list all element counts.
 75 
 76         >>> Counter('abcdeabcdabcaba').most_common(3)
 77         [('a', 5), ('b', 4), ('c', 3)]
 78 
 79         '''
 80         # Emulate Bag.sortedByCount from Smalltalk
 81         if n is None:
 82             return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)
 83         return _heapq.nlargest(n, self.iteritems(), key=_itemgetter(1))
 84 
 85     def elements(self):
 86         """ 计数器中的所有元素,注:此处非所有元素集合,而是包含所有元素集合的迭代器 """
 87         '''Iterator over elements repeating each as many times as its count.
 88 
 89         >>> c = Counter('ABCABC')
 90         >>> sorted(c.elements())
 91         ['A', 'A', 'B', 'B', 'C', 'C']
 92 
 93         # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1
 94         >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
 95         >>> product = 1
 96         >>> for factor in prime_factors.elements():     # loop over factors
 97         ...     product *= factor                       # and multiply them
 98         >>> product
 99 
100         Note, if an element's count has been set to zero or is a negative
101         number, elements() will ignore it.
102 
103         '''
104         # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
105         return _chain.from_iterable(_starmap(_repeat, self.iteritems()))
106 
107     # Override dict methods where necessary
108 
109     @classmethod
110     def fromkeys(cls, iterable, v=None):
111         # There is no equivalent method for counters because setting v=1
112         # means that no element can have a count greater than one.
113         raise NotImplementedError(
114             'Counter.fromkeys() is undefined.  Use Counter(iterable) instead.')
115 
116     def update(self, iterable=None, **kwds):
117         """ 更新计数器,其实就是增加;如果原来没有,则新建,如果有则加一 """
118         '''Like dict.update() but add counts instead of replacing them.
119 
120         Source can be an iterable, a dictionary, or another Counter instance.
121 
122         >>> c = Counter('which')
123         >>> c.update('witch')           # add elements from another iterable
124         >>> d = Counter('watch')
125         >>> c.update(d)                 # add elements from another counter
126         >>> c['h']                      # four 'h' in which, witch, and watch
127 
128         '''
129         # The regular dict.update() operation makes no sense here because the
130         # replace behavior results in the some of original untouched counts
131         # being mixed-in with all of the other counts for a mismash that
132         # doesn't have a straight-forward interpretation in most counting
133         # contexts.  Instead, we implement straight-addition.  Both the inputs
134         # and outputs are allowed to contain zero and negative counts.
135 
136         if iterable is not None:
137             if isinstance(iterable, Mapping):
138                 if self:
139                     self_get = self.get
140                     for elem, count in iterable.iteritems():
141                         self[elem] = self_get(elem, 0) + count
142                 else:
143                     super(Counter, self).update(iterable) # fast path when counter is empty
144             else:
145                 self_get = self.get
146                 for elem in iterable:
147                     self[elem] = self_get(elem, 0) + 1
148         if kwds:
149             self.update(kwds)
150 
151     def subtract(self, iterable=None, **kwds):
152         """ 相减,原来的计数器中的每一个元素的数量减去后添加的元素的数量 """
153         '''Like dict.update() but subtracts counts instead of replacing them.
154         Counts can be reduced below zero.  Both the inputs and outputs are
155         allowed to contain zero and negative counts.
156 
157         Source can be an iterable, a dictionary, or another Counter instance.
158 
159         >>> c = Counter('which')
160         >>> c.subtract('witch')             # subtract elements from another iterable
161         >>> c.subtract(Counter('watch'))    # subtract elements from another counter
162         >>> c['h']                          # 2 in which, minus 1 in witch, minus 1 in watch
163         >>> c['w']                          # 1 in which, minus 1 in witch, minus 1 in watch
164         -1
165 
166         '''
167         if iterable is not None:
168             self_get = self.get
169             if isinstance(iterable, Mapping):
170                 for elem, count in iterable.items():
171                     self[elem] = self_get(elem, 0) - count
172             else:
173                 for elem in iterable:
174                     self[elem] = self_get(elem, 0) - 1
175         if kwds:
176             self.subtract(kwds)
177 
178     def copy(self):
179         """ 拷贝 """
180         'Return a shallow copy.'
181         return self.__class__(self)
182 
183     def __reduce__(self):
184         """ 返回一个元组(类型,元组) """
185         return self.__class__, (dict(self),)
186 
187     def __delitem__(self, elem):
188         """ 删除元素 """
189         'Like dict.__delitem__() but does not raise KeyError for missing values.'
190         if elem in self:
191             super(Counter, self).__delitem__(elem)
192 
193     def __repr__(self):
194         if not self:
195             return '%s()' % self.__class__.__name__
196         items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
197         return '%s({%s})' % (self.__class__.__name__, items)
198 
199     # Multiset-style mathematical operations discussed in:
200     #       Knuth TAOCP Volume II section 4.6.3 exercise 19
201     #       and at http://en.wikipedia.org/wiki/Multiset
202     #
203     # Outputs guaranteed to only include positive counts.
204     #
205     # To strip negative and zero counts, add-in an empty counter:
206     #       c += Counter()
207 
208     def __add__(self, other):
209         '''Add counts from two counters.
210 
211         >>> Counter('abbb') + Counter('bcc')
212         Counter({'b': 4, 'c': 2, 'a': 1})
213 
214         '''
215         if not isinstance(other, Counter):
216             return NotImplemented
217         result = Counter()
218         for elem, count in self.items():
219             newcount = count + other[elem]
220             if newcount > 0:
221                 result[elem] = newcount
222         for elem, count in other.items():
223             if elem not in self and count > 0:
224                 result[elem] = count
225         return result
226 
227     def __sub__(self, other):
228         ''' Subtract count, but keep only results with positive counts.
229 
230         >>> Counter('abbbc') - Counter('bccd')
231         Counter({'b': 2, 'a': 1})
232 
233         '''
234         if not isinstance(other, Counter):
235             return NotImplemented
236         result = Counter()
237         for elem, count in self.items():
238             newcount = count - other[elem]
239             if newcount > 0:
240                 result[elem] = newcount
241         for elem, count in other.items():
242             if elem not in self and count < 0:
243                 result[elem] = 0 - count
244         return result
245 
246     def __or__(self, other):
247         '''Union is the maximum of value in either of the input counters.
248 
249         >>> Counter('abbb') | Counter('bcc')
250         Counter({'b': 3, 'c': 2, 'a': 1})
251 
252         '''
253         if not isinstance(other, Counter):
254             return NotImplemented
255         result = Counter()
256         for elem, count in self.items():
257             other_count = other[elem]
258             newcount = other_count if count < other_count else count
259             if newcount > 0:
260                 result[elem] = newcount
261         for elem, count in other.items():
262             if elem not in self and count > 0:
263                 result[elem] = count
264         return result
265 
266     def __and__(self, other):
267         ''' Intersection is the minimum of corresponding counts.
268 
269         >>> Counter('abbb') & Counter('bcc')
270         Counter({'b': 1})
271 
272         '''
273         if not isinstance(other, Counter):
274             return NotImplemented
275         result = Counter()
276         for elem, count in self.items():
277             other_count = other[elem]
278             newcount = count if count < other_count else other_count
279             if newcount > 0:
280                 result[elem] = newcount
281         return result
282 
283 Counter
Counter

相关文章: