class - object

 

 

 

 


Python release object

 

# 建立 list

>>> foo = ['bar' for _ in xrange(10000000)]

# Checking

ps aux | grep python$

root     29411  1.8  0.5  97536 86328 pts/10   S+   17:55   0:00 python

# Release

del foo

# Checking

ps aux | grep python$

root     29411  0.8  0.0  17916  7980 pts/10   S+   17:55   0:00 python

 


class_collections

 

collections - High-performance container datatypes

 

defaultdict([default_factory])

* Returns a new dictionary-like object.

default_factory

This attribute is used by the __missing__() method;

Example:

>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
...     d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]

When each key is encountered for the first time, it is not already in the mapping;
so an entry is automatically created using the default_factory function which returns an empty list. The list.append() operation then attaches the value to the new list.

>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
...     d[k] += 1
...
>>> d.items()
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]

 


 

 

 

 

 

 

 

 

Creative Commons license icon Creative Commons license icon