The document discusses the deque collection in Python. Some key points:
- Deque allows fast appends and pops from either side of the list, with O(1) time complexity, unlike regular lists which are slow (O(n)) for pop(0) and insert(0,v).
- Deque provides methods like append, appendleft, popleft, pop for adding/removing elements from either side of the list.
- It can be initialized with a maximum length to act as a sliding window, discarding old elements as new ones are added.
- Methods like rotate rotate the deque a given number of positions, extending adds multiple elements at once. Deque is useful when
45. Subclassing dict de
collections.MutableMapping
from collections import MutableMapping
class Dicto(MutableMapping):
def __init__(self):
self.d = {} >>> d = Dicto()
>>> d[’eggs’] = ’ovos’
def __delitem__(self, i): >>> d[’ham’] = ’presunto’
del self.d[i] >>> for i in d:
... print i + ’:’ + d[i]
def __setitem__(self, k, v): eggs:ovos
self.d[k.lower()] = v ham:presunto
>>> d.get(’HaM’)
def __getitem__(self, k): presunto
return self.d[k.lower()] >>> d[’eGGs’]
’ovos’
def __iter__(self):
return iter(self.d)
def __len__(self):
return len(self.d)
46. Subclassing dict de
collections.MutableMapping
from collections import MutableMapping
class Dicto(MutableMapping):
def __init__(self):
self.d = {} >>> d = Dicto()
>>> d[’eggs’] = ’ovos’
def __delitem__(self, i): >>> d[’ham’] = ’presunto’
del self.d[i] >>> for i in d:
... print i + ’:’ + d[i]
def __setitem__(self, k, v): eggs:ovos
self.d[k.lower()] = v ham:presunto
>>> d.get(’HaM’)
def __getitem__(self, k): presunto
return self.d[k.lower()] >>> d[’eGGs’]
’ovos’
def __iter__(self):
return iter(self.d)
def __len__(self):
return len(self.d)
47. Referência
• Lightning Talk de:
no fisl 2011.
• The Data Structures of Python, Alex Gaynor no PyCon2011
• http://www.doughellmann.com/PyMOTW/abc/index.html
• http://docs.python.org/library/collections.html
48. Dúvidas?
André Ericson
http://www.github.com/aericson
de.ericson@gmail.com
@_aericson