3.9. Other Python Sequence TypesΒΆ
There are number of modules that add specialized sequence data structures often useful in certain situations.
- The collectionsmodule from the standard module, which includes
- deque, a list-like structure which can efficiently append objects to either ends.- In [1]: from collections import deque In [2]: d = deque([1,2,3]) In [3]: d.append(4) In [4]: d.appendleft(0) In [5]: d Out[5]: deque([0, 1, 2, 3, 4]) 
- nametuple, a tuple that allows efficient accessing records by name.
 
 
- The 
- The numpynumerical library provides
- arrays and matrices for efficient numerical computations - In [6]: import numpy as np In [7]: v = np.array([1,2,3]) In [8]: v Out[8]: array([1, 2, 3]) In [9]: m = np.matrix([[1,2],[3,4]]) In [10]: m Out[10]: matrix([[1, 2], [3, 4]]) 
- functions that can be applied to all elements of a - numpyarray.- In [11]: np.cos(v) Out[11]: array([ 0.54030231, -0.41614684, -0.9899925 ]) 
 
 
- The 
Note
If you are interested in learning more about numpy, there is a online
textbook by Nicolas Rougier, which includes a bibliography of other numpy resources.
- The pandaslibrary provides
- data frames similar in construction and usage to the - data.framefrom R- In [12]: import pandas as pd In [13]: d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']), ....: 'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])} ....: In [14]: df = pd.DataFrame(d) In [15]: df Out[15]: one two a 1.0 1.0 b 2.0 2.0 c 3.0 3.0 d NaN 4.0 In [16]: pd.DataFrame(d, index=['d', 'b', 'a']) 
 
 
- The