Python Collections

Collections

High-performance container datatypes

   * nametuple()
   * deque
   * Counter
   * OrderedDict
   * defaultdict

一、Counter

dict subclass for counting hash able objects


Python 2.7.2 (default, Jun 20 2012, 16:23:33)

[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> from collections import Counter

>>> cnt = Counter()

>>> dir(cnt)

['__add__', '__and__', '__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__missing__', '__module__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__weakref__', 'clear', 'copy', 'elements', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'most_common', 'pop', 'popitem', 'setdefault', 'subtract', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']



>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:

...     cnt[word] += 1

...

>>> cnt

Counter({'blue': 6, 'red': 4, 'green': 2})




1、elements
>>> list(cnt.elements())
['blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'green', 'green', 'red', 'red', 'red', 'red']




2、most_common([n])
>>> cnt.most_common(3);
[('blue', 6), ('red', 4), ('green', 2)]



3、subtract

>>> cnt2=Counter(blue=1,red=2)
>>> cnt.subtract(cnt2)
>>> cnt
Counter({'blue': 5, 'green': 2, 'red': 2, 'orange': 1})




4、fromkeys

not implemented.



5、update

>>> cnt.update({'orange'})

>>> cnt

Counter({'blue': 6, 'red': 4, 'green': 2, 'orange': 1})






二、deque



>>> from collections import deque
>>> dir(deque)
['__class__', '__copy__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'appendleft', 'clear', 'count', 'extend', 'extendleft', 'maxlen', 'pop', 'popleft', 'remove', 'reverse', 'rotate']




1、append

2、appendleft(x)

3、clear

4、count(x)

5、extend(iterable)

6、extendleft(iterable)

7、pop()

8、popleft()

9、remove(value)

10、reverse()

11、rotate(n)

12、maxlen



三、defaultdict



dict's subclass

使用defaultdict方法可以轻松定义一个树的数据结构



from collections import defaultdict

import simplejson as json

def tree():

     return defaultdict(tree)



def dicts(t):

     return {k: dicts(t[k]) for k in t}  if isinstance(t, defaultdict) else t



users = tree()

users['good']['username']='eric'

users['bad']['username']='ricky'

print (json.dumps(users))

print dicts(users)




{"bad": {"username": "ricky"}, "good": {"username": "eric"}}

{'bad': {'username': 'ricky'}, 'good': {'username': 'eric'}}





四、namedtuple

from collections import named tuple

Point = namedtuple('Point','x y',verbose=True)

point = Point(1,2)

print point



class Point(tuple):
        'Point(x, y)'

        __slots__ = ()

        _fields = ('x', 'y')

        def __new__(_cls, x, y):

            'Create new instance of Point(x, y)'

            return _tuple.__new__(_cls, (x, y))

        @classmethod

        def _make(cls, iterable, new=tuple.__new__, len=len):

            'Make a new Point object from a sequence or iterable'

            result = new(cls, iterable)

            if len(result) != 2:

                raise TypeError('Expected 2 arguments, got %d' % len(result))

            return result

        def __repr__(self):

            'Return a nicely formatted representation string'

            return 'Point(x=%r, y=%r)' % self

        def _asdict(self):

            'Return a new OrderedDict which maps field names to their values'

            return OrderedDict(zip(self._fields, self))

        def _replace(_self, **kwds):

            'Return a new Point object replacing specified fields with new values'

            result = _self._make(map(kwds.pop, ('x', 'y'), _self))

            if kwds:

                raise ValueError('Got unexpected field names: %r' % kwds.keys())

            return result

        def __getnewargs__(self):

            'Return self as a plain tuple.  Used by copy and pickle.'

            return tuple(self)

        x = _property(_itemgetter(0), doc='Alias for field number 0')

        y = _property(_itemgetter(1), doc='Alias for field number 1')




五、OrderedDict

In [15]: from collections import OrderedDict


In [16]: d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}



In [17]: OrderedDict(sorted(d.items(), key=lambda t: t[0]))

Out[17]: OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])



In [18]: OrderedDict(sorted(d.items(), key=lambda t: t[1]))

Out[18]: OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])



In [19]: OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))

Out[19]: OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])







参考资料:

http://docs.python.org/2/library/collections.html


已有 0 人发表留言,猛击->>这里<<-参与讨论


ITeye推荐



posted @ 2012-12-24 19:12  1Q84  阅读(288)  评论(0编辑  收藏  举报