我有一个名为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']
我想创建一个名为closing_prices的新列表,其中只有上面列表中的收盘价,我发现它是从上面列表中的元素10开始的每第6个元素。
到目前为止,这是我的代码:
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
这给出了这个结果:
['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
显然我想要的是最后一行:
['880.37', '877.07', '873.65', '866.20', '869.79', '880.93', '884.74']
但是我一直在列表索引超出范围,因为我想当你在stock_data中为x做它只是通过列表直到它没有任何问题到达终点?为什么要走出指数?
Python 3,谢谢。
答案 0 :(得分:4)
显然,你在前7次迭代中做了你想要的。但是在完成第7次迭代之后,for循环仍然只遍历列表中更多元素中的7个,因此它将尝试访问stock_data[10+6*7]
。你的意思是:
closing_prices = stock_data[10::6]
stock_data[a:b:c]
从索引stock_data
开始返回a
的子列表,从第c
个元素开始,但不包括索引b
。如果未指定,则默认为a=0
,c=1
,b=(length of the list)
。这称为切片。
答案 1 :(得分:3)
# 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
输出:
['880.37', '877.07', '873.65', '866.20', '869.79', '880.93', '884.74']
答案 2 :(得分:1)
简单的解决方法是将你的追加包装在try catch语句中。
for item in stock_data:
try:
closing_prices.append(stock_data[count])
except IndexError:
break
print (closing_prices)
count = count + 6
你得到错误的原因是,当你到达列表中的第5个到最后一个项目然后向它添加6时,你现在已经超出列表最大索引的范围了。
另一种可能的解决方案是使用while循环。
closing_prices = []
count = 10
while count < len(stock_data):
closing_prices.append(stock_data[count])
count += 6
print closing_prices
答案 3 :(得分:1)
实际上,您可以在一行代码中编写它:
您的数据:
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'
]
您的代码:
print [stock_data[i] for i in xrange(10, len(stock_data) - 1, 6)]
您的输出:
['880.37', '877.07', '873.65', '866.20', '869.79', '880.93', '884.74']