我运行代码时出现以下错误
Traceback (most recent call last):
File "E:\scanned\als course\12 15 14 the al project\index errot.py", line 80, in <module>
sym_index = sym_list.index(y)
AttributeError: 'generator' object has no attribute 'index'
>>>
我对python很陌生,我认为&#34;我已正确声明列表并且列表具有索引属性?我只是不明白什么是错的任何帮助都会受到赞赏我也希望我能更好地格式化这个问题
数据的子集
2015-08-04 02:14:05.249392,AA,0.0193103612,0.0193515212,0.0249713335,30.6542480634,30.7195875454,39.640763021,0.2131498442,29.0406746589,13524.5347810182,89,57,99
2015-08-04 02:14:05.325113,AAPL,0.0170506271,0.0137941891,0.0105915637,27.0670313481,21.8975963326,16.8135861893,-19.0986405157,-23.2172064279,21.5647072302,33,26,75
2015-08-04 02:14:13.596026,YUM,0.0150960249,0.0096451121,0.0059497165,23.9641966195,15.3111408421,9.4448821537,-36.1082656547,-38.3136615938,6.1077315656,10,11,70
2015-08-05 01:00:01.578356,AA,0.0195344666,0.0197647353,0.0216936719,31.0100044039,31.375544675,34.4376364665,1.178781745,9.7594856861,727.9298290159,83,53,98
2015-08-05 01:00:01.653702,AAPL,0.0182801429,0.0153930649,0.0129012765,29.0188272234,24.4357329651,20.4801414799,-15.7935199208,-16.1877341304,2.4960503525,55,30,69
2015-08-05 01:00:11.685967,YUM,0.015053783,0.0095630245,0.0045548817,23.8971396792,15.1808306936,7.2306505541,-36.4742772674,-52.3698623612,43.5802606239,11,10,74
2015-08-06 01:00:01.580982,AA,0.016405504,0.0187336551,0.0138878945,26.0429302344,29.7387555321,22.0463490932,14.1912805682,-25.8666050455,-282.2711130351,50,69,35
2015-08-06 01:00:01.657591,AAPL,0.0177744137,0.0123407978,0.0155526393,28.2160070183,19.590409183,24.6890494178,-30.5698741487,26.0262059213,-185.1367781062,97,9,44
2015-08-06 01:00:14.071073,YUM,0.0134016613,0.009754587,0.0046291687,21.2744777757,15.4849268147,7.3485774914,-27.2135984817,-52.543673087,93.07874011,19,12,83
2015-08-07 01:00:02.111758,AA,0.0167336443,0.0195330063,0.0157903576,26.5638368026,31.0076862614,25.0664156097,16.7289442857,-19.1606384353,-214.5358494121,42,79,34
2015-08-07 01:00:02.426008,AAPL,0.0172853837,0.01222536,0.0159302942,27.4396959903,19.4071573379,25.2885580713,-29.2734243675,30.3053179346,-203.525018304,96,10,35
2015-08-07 01:00:20.003695,YUM,0.0113498677,0.007539233,0.0090156499,18.0173564022,11.9681613499,14.3119005831,-33.5742653767,19.583118615,-158.3277650167,69,8,34
导致错误的代码
import csv
from datetime import datetime
num_sym = 100
num_days = 252
sym_list = []
date_list = []
sym_index = 0
date_index = 0
hvabs_tab = [[0 for x in range(num_sym)] for y in range(num_days)]
dates_t = []
def dedupe(items):
seen = set()
for item in items:
if item not in seen:
yield item
seen.add(item)
return item
def parse_ymd(s):
date_bit = s.split('-')
long_date = date_bit[0]+":" + date_bit[1]+":" + date_bit[2] # formatted 2015:08:05
#print('y',date_bit[0],'m',date_bit[1],'d',date_bit[2],'all',date_bit)
#y 2015 m 08 d 04 all ['2015', '08', '04']
return long_date # formatted 2015:08:05
with open('hvanal2015s.csv') as f:
f_csv = csv.reader(f)
for x in f_csv:
for i,y in enumerate(x):
if i == 1 :
sym_list.append(y) # grabbing ALL the symbols shoving them in list
if i == 0 :
date_line = str(x[0])
date_t = date_line.split()
just_date = parse_ymd(date_t[0])
date_list.append(just_date) # grabbing ALL dates shoving them in list
sym_list = dedupe(sym_list)
date_list = dedupe(date_list)
print('sym list final',*sym_list)
print('date list final',*date_list)
print('---------------------')
f.seek(0) # resetting the csv iterator to top of file to process the ranks now we have
# the lists for sym and dates populated
for day_data in f_csv:
for i,y in enumerate(day_data):
if i == 1 : # found symbol
sym_index = sym_list.index(y)
print('y and sym index',y,sym_index)
# if i == 0 : # found related date
#date_index = date_list.index(y)
# print('y and date index',y,date_index)
答案 0 :(得分:1)
注意dedupe
:
def dedupe(items):
seen = set()
for item in items:
if item not in seen:
yield item
seen.add(item)
return item
查看yield
关键字?这使得返回值为generator
(可迭代),但它不是list
。您可以使用内置list
callable从生成器轻松构造list
:
sym_list = list(dedupe(sym_list))
另外,请注意您的dedupe
生成器函数有一个return
语句,但除了欺骗您的代码读者以外,它并没有真正做任何事情,而是认为{{1 }}不是生成器: - )