Search code examples
pythonnumpypython-2.7nested-lists

Arithmetic Operations with Nested Lists in Python


I am attempting to subtract values in a nested list (a list of historical stock price data from Yahoo finance) and I have been running into problems. I am attempting simple subtraction (i.e. high - low), but I am unable to implement this. I am probably missing something fundamental on the nature of lists, but I am stumped.

An example of the nested list I am using:

[['2012-07-31', '16.00', '16.06', '15.81', '15.84', '13753800', '15.8'],
 ['2012-07-30', '16.15', '16.15', '15.90', '15.98', '10187600', '15.9'],
 ['2012-07-27', '15.88', '16.17', '15.84', '16.11', '14220800', '16.1'],
 ['2012-07-26', '15.69', '15.88', '15.62', '15.80', '11033300', '15.8'],
 ['2012-07-25', '15.52', '15.64', '15.40', '15.50', '15092000', '15.5'],
 ['2012-07-24', '15.74', '15.76', '15.23', '15.43', '19733400', '15.4'],
 ['2012-07-23', '15.70', '15.81', '15.59', '15.76', '14825800', '15.7'],
 ['2012-07-20', '15.75', '15.94', '15.68', '15.92', '16919700', '15.9'],
 ['2012-07-19', '15.71', '15.86', '15.64', '15.73', '15985300', '15.7'],
 ...]

I want to subtract the 4th 'column' from the third 'column' and populate another list with the results (order IS important.) What is the best way to implement this?


Solution

  • In native Python, if you want to leave the nested list (call it 'table'; each list within it is 'row') intact, the concise, idiomatic way to create a list of differences is:

    differences = [float(row[3]) - float(row[4]) for row in table]
    

    so that differences[i] == table[i][3] - table[i][4].

    If the numeric data in the table will be used by other code, you might want to convert the strings to floats within the table:

    table = [[r[0], float(r[1]), float(r[2]),
             float(r[3]), float(r[4]), r[5], float(r[6])] for r in table]
    

    so that the differences table would just be created by

    differences = [r[3] - r[4] for r in table]