Making a list of data structures for reference. I probably won’t finish this. I mainly was trying to remember what ‘structured array’ was called and decided to make a list and maybe investigate a couple of them. In the process of searching for it I found ‘namedtuple’ which looks very useful.
—
List
Stack (list)
Queue (list)
Tuple
Namedtuple
# IMPORT NAMEDTUPLE
from collections import namedtuple
# CREATE A NAMEDTUPLE STRUCTURE
Rock = namedtuple('Rock', 'q w e r t')
# CREATE AN INSTANCE
n2 = Rock(5,4,3,2,1)
# SAME AS
n2 = Rock(t=1, e=3, q=5, r=2, w=4)
# CHANGE A VALUE BY COPYING TO NEW INSTANCE
n2 = n2._replace(w=234, r=[3,4,5])
Deque
Set
Frozenset
Dictionary
Counter (dict)
OrderedDict (dict)
Array
Structured Array
# IMPORT NUMPY ARRAY
from numpy import array
# CREATE A DATA LIST, TUPLE VALUES WILL RECEIVE NAMES
dl = [[(1,2,3,4),(3,2,5,6),(7,6,9,8)],[(1,2,3,4),(3,2,5,6),(7,6,9,8)]]
# CREATE THE DATA TYPE LIST
dt = [('1st','float'),('2nd','int'),('3rd','float'),('4th','int')]
# CREATE A STRUCTURED ARRAY, TUPLES BECOME NP.VOID OBJECTS
d = array(dl, dtype=dt)
# SET ALL VALUES OF A CATEGORY
d['1st'] = 6
# SET ONE VALUE
d['1st'][0,0] = 5
# same as
d[0,0]['1st'] = 5
# GET LISTING OF DTYPE, SIMILAR TO: print dt
print d.dtype
# JUST GET CATEGORY NAMES
print d.dtype.names
# GET A DICTIONARY OF CATEGORY(KEYS) WITH THE DATA TYPE AND BYTE POSITION (I BELIEVE)
print d.dtype.fields
Matrix
Vector
Create a class