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']

我想创建一个名为 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,谢谢。

4

4 回答 4

4

它显然在前 7 次迭代中完成了您想要的操作。但是在完成第 7 次迭代后,for 循环仍将只遍历列表中更多元素中的 7 个,因此它将尝试访问stock_data[10+6*7]. 你的意思可能是:

closing_prices = stock_data[10::6]

stock_data[a:b:c]返回stock_data从 index 开始的子列表a,取每个cth 元素,直到但不包括 index b。如果未指定,则默认为a=0, c=1, b=(length of the list)。这称为切片

于 2013-06-29T23:34:25.567 回答
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']
于 2013-06-29T23:48:46.980 回答
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
于 2013-06-29T23:31:58.143 回答
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']
于 2013-06-29T23:32:13.933 回答