2

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?

4 Answers 4

3

You can use a list comprehension:

from decimal import Decimal
result = [(row[0], Decimal(row[2]) - Decimal(row[3])) for row in data]
Sign up to request clarification or add additional context in comments.

Comments

1

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]

1 Comment

This works exactly the way I wanted it to. I should have been more explicit by stating that the data would be used by other code. Thank you.
1

The best way to do this would be to use numpy, python was never designed to work with large amounts of data, numpy is, being that a fair amount of its subroutines are implemented using other languages compiled to native binaries, and it can use accelerated linear algebra libraries to really speed up the computations.

heres a quick example:

>>> import numpy
>>> values = numpy.random.rand(5, 5) # 5 by 5 matrix with random values
>>> values[:, 3] - values[:, 2] # numpy is 0 index, so the fourth column is 3 and the third is 2

Comments

1

Your first problem is that your lists contain strings and not numbers. If you want to subtract the numbers you'll need to convert them to numbers (by using, e.g., float at the time you create your lists).

It seems you want to add and subtract the numbers component-wise, treating your data as a table with rows and columns. For that, you should use numpy, which is a library designed for this. You might also want to look at pandas, which is a library that builds on numpy to provide powerful features for slicing and dicing data by rows and columns. (If you happen to be reading your data from a file, these libraries also provide some tools that will let you read, e.g., a CSV file and import the data as numbers and/or dates rather than strings.)

1 Comment

Thanks for your comment. I will certainly look into pandas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.