Python 3

home

Building Multidimensional Containers

Multidimensional structures - building

Usually, we don't initialize multidimensional structures within our code. They are usually read from .json files.


Most commonly, we will build a multidimensional structure of our own design based on the data we are trying to store. For example, we may use the Fama-French file to build a dictionary of lists - the key of the dictionary being the date, and the value being a 4-element list of the values for that date.

outer_dict = {}                            # new dict
for line in open('FF_abbreviated.txt').read().splitlines():
    columns = line.split()                 # split each line into
                                           # a list of string values

    date = columns[0]                      # the first value is the date
    values = columns[1:]                   # slice this list into a
                                           # list of floating-point
                                           # values

    outer_dict[date] = values              # so values is a
                                           # list, assigned as value
                                           # to key date




Building a List of Lists

A list of lists provides a "matrix" structure similar to an Excel spreadsheet.


value_table =       [
                       [ '19260701', 0.09, -0.22, -0.30, 0.009 ],
                       [ '19260702', 0.44, -0.35, -0.08, 0.009 ],
                       [ '19260703', 0.17, 0.26,  -0.37, 0.009 ]

                    ]

Probably used more infrequently, a list of lists allows us to access values through list methods (looping and indexed subscripts). The "outer" list has 3 items -- each items is a list, and each list represents a row of data. Each row list has 4 items, which represent the row data from the Fama-French file: the date, the Mkt-RF, SMB, HML and RF values. Building this structure is simplified in that the 'inner' list is produced from the split():


outer_list = []                            # new list

for line in open('FF_abbreviated.txt').read().splitlines():

    columns = line.split()                 # split row into list of strings
    outer_list.append(columns)             # append row list to outer list




Building a List of Dicts

A list of dicts structures tabular rows into field-keyed dictionaries.


value_table = [
                { 'date': '19260701', 'MktRF': 0.09, 'SMB': -0.22,
                                      'HML': -0.30, 'RF': 0.009 },

                { 'date': '19260702', 'MktRF': 0.44, 'SMB': -0.35,
                                      'HML': -0.08, 'RF': 0.009 },

                { 'date': '19260706', 'MktRF': 0.17, 'SMB': 0.26,
                                      'HML': -0.37, 'RF': 0.009 }
              ]

The "outer" list contains 3 items, each being a dictionary with identical keys. The keys in each dict correspond to field / column labels from the table, so it's easy to identify and access a given value within a row dict.


Building this structure simply means constructing a new dict for each row. This does not have to be done piecemeal, but as a single initialized dictionary:

outer_list = []                            # new list

for line in open('FF_abbreviated.txt').read().splitlines():

    columns = line.split()                 # split row into list of strings

    inner_dict = { 'date':  columns[0],
                   'MktRF': columns[1],
                   'SMB':   columns[2],
                   'HML':   columns[3],
                   'RF':    columns[4]  }

    outer_list.append(inner_dict)




Building a Dict of Dicts

In a dict of dicts, each unique key points to another dict with keys and values.


date_values = {
    '19260701':   { 'MktRF':  0.09,
                    'SMB':   -0.22,
                    'HML':   -0.30,
                    'RF':    0.009 },
    '19260702':   { 'MktRF':  0.44,
                    'SMB':   -0.35,
                    'HML':   -0.08,
                    'RF':    0.009 },
}

The "outer" dict contains string keys, each of which is associated with a dictionary -- each "inner" dictionary is a convenient key/value access to the fields of the table, as we had with a list of dicts.


Similar to the list of dicts, we can very simply build an 'inner' dict inside the loop and associate it with a given key:

outer_dict = {}                            # new dict

for line in open('FF_abbreviated.txt').read().splitlines():

    columns = line.split()                 # split row into list of strings

    inner_dict = { 'MktRF': columns[1],
                   'SMB':   columns[2],
                   'HML':   columns[3],
                   'RF':    columns[4]  }

    outer_dict[columns[0]] = inner_dict




Building a Dict of Lists

A dict of lists allows association of a sequence of values with unique keys.


value_table = {  '1926': [  0.09,  0.44,  0.17, -0.15, -0.06, -0.55,
                            0.61,  0.05, 0.51 ],

                 '1927': [  -0.97,  0.30,  0.13, -0.18,  0.31,  0.39,
                            0.14, -0.27, 0.05 ],

                 '1928': [  0.43, -0.14, -0.71,  0.61,  0.13, -0.88,
                            -0.85,  0.12, 0.48 ]                       }

The "outer" dict contains 3 string keys, each associated with a list of float values -- in this case, the MktRF values from each of the trading days for each year (only the first 9 are included here for clarity). To produce a structure like this we need only consider the counting or summing dictionary. This structure associates a list with each key, so instead of summing a new value to the current value associated with each key, we can append the new value to the list associated with the key.





Writing to JSON

To write to JSON we simply open a file for writing and dump the structure to the file.


Here is some simple JSON with an arbitrary structure:

struct = {
           "key1":  ["a", "b", "c"],
           "key2":  {
                      "innerkey1": 5,
                      "innerkey2": "woah"
                    },
           "key3":  55.09,
           "key4":  "hello"
        }

We can 'dump' this structure to a file directly, or to a string:

fh = open('mystruct.json' ,'w')
mys = json.dump(struct, fh, indent=4)      # write struct to a file
fh.close()


struct_str = json.dumps(struct, indent=4)  # write struct to a string

fh = open('mystruct.json', 'w')
fh.write(struct_str)                       # write string to the file
fh.close()




[pr]