Reputation: 2250
I have a list called stock_data which contains this data:
['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close\n2013-06-28', '874.90', '881.84', '874.19', '880.37', '2349300', '880.37\n2013-06-27', '878.80', '884.69', '876.65', '877.07', '1926500', '877.07\n2013-06-26', '873.75', '878.00', '870.57', '873.65', '1831400', '873.65\n2013-06-25', '877.26', '879.68', '864.51', '866.20', '2553200', '866.20\n2013-06-24', '871.88', '876.32', '863.25', '869.79', '3016900', '869.79\n2013-06-21', '888.34', '889.88', '873.07', '880.93', '3982300', '880.93\n2013-06-20', '893.99', '901.00', '883.31', '884.74', '3372000', '884.74\n']
I want to make a new list called closing_prices which has only the closing prices in the above list which I found to be are every 6th element starting from element 10 in the above list.
Here is my code so far:
stock_data = []
for line in data:
stock_data.append(line)
closing_prices= []
count = 10
for item in stock_data:
closing_prices.append(stock_data[count])
print (closing_prices)
count = count + 6
Which gives this result:
['880.37']
['880.37', '877.07']
['880.37', '877.07', '873.65']
['880.37', '877.07', '873.65', '866.20']
['880.37', '877.07', '873.65', '866.20', '869.79']
['880.37', '877.07', '873.65', '866.20', '869.79', '880.93']
['880.37', '877.07', '873.65', '866.20', '869.79', '880.93', '884.74']
Traceback (most recent call last):
File "C:\Users\Usman\Documents\Developer\Python\Pearson Correlation\pearson_ce.py", line 34, in <module>
closing_prices.append(stock_data[count])
IndexError: list index out of range
Obviously what I want is the last line:
['880.37', '877.07', '873.65', '866.20', '869.79', '880.93', '884.74']
But I've been scratching my head at the list index out of range because I thought when you do for x in stock_data it just goes through the list until it reaches the end without any problems? Why is going out of the index?
Python 3, thanks.
Upvotes: 1
Views: 113
Reputation: 97938
# for splitting adj-close/date @ the newlines
stock_data = [ y for x in stock_data for y in x.split('\n') ]
headers = { k:i for i,k in enumerate(stock_data[:7]) }
# convert stock_data to a matrix
stock_data = zip(*[iter(stock_data[7:])]*len(headers))
# chose closing column
closing = [ r[headers['Close']] for r in stock_data ]
print closing
Output:
['880.37', '877.07', '873.65', '866.20', '869.79', '880.93', '884.74']
Upvotes: 3
Reputation: 7379
It evidently does what you want in the first 7 iterations. But after completing the 7th iteration, the for loop will still only have traversed 7 of the many more elements in the list, and so it will then try to access stock_data[10+6*7]
. What you probably meant is:
closing_prices = stock_data[10::6]
stock_data[a:b:c]
returns a sublist of stock_data
beginning at index a
, taking every c
th element, up to but not including index b
. If unspecified, they default to a=0
, c=1
, b=(length of the list)
. This is known as slicing.
Upvotes: 4
Reputation: 12150
Actually, you can write it in one line of code:
Your data:
stock_data = [
'Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close\n2013-06-28',
'874.90', '881.84', '874.19', '880.37', '2349300', '880.37\n2013-06-27',
'878.80', '884.69', '876.65', '877.07', '1926500', '877.07\n2013-06-26',
'873.75', '878.00', '870.57', '873.65', '1831400', '873.65\n2013-06-25',
'877.26', '879.68', '864.51', '866.20', '2553200', '866.20\n2013-06-24',
'871.88', '876.32', '863.25', '869.79', '3016900', '869.79\n2013-06-21',
'888.34', '889.88', '873.07', '880.93', '3982300', '880.93\n2013-06-20',
'893.99', '901.00', '883.31', '884.74', '3372000', '884.74\n'
]
Your code:
print [stock_data[i] for i in xrange(10, len(stock_data) - 1, 6)]
Your output:
['880.37', '877.07', '873.65', '866.20', '869.79', '880.93', '884.74']
Upvotes: 1
Reputation: 13699
The simple fix is to wrap your append in a try catch statement.
for item in stock_data:
try:
closing_prices.append(stock_data[count])
except IndexError:
break
print (closing_prices)
count = count + 6
the reason you are getting to the error is that when you get to the 5th to the last item in the list then add 6 to it you are now out of range of the lists maximum index so to speak.
Another possible solution is to use a while loop.
closing_prices = []
count = 10
while count < len(stock_data):
closing_prices.append(stock_data[count])
count += 6
print closing_prices
Upvotes: 1