在python中列出索引超出范围
我写了一个这样的程序:在python中列出索引超出范围,python,numpy,python-2.7,Python,Numpy,Python 2.7,我写了一个这样的程序: reader=csv.reader(open("lrgdata.csv")) headers = reader.next() Amt_Wtotal=0 Amt_Dtotal=0 dataW =[] dataD=[] counts_W=defaultdict(int) counts_D=defaultdict(int) for row in reader: if(row[28]=='W'): c
reader=csv.reader(open("lrgdata.csv"))
headers = reader.next()
Amt_Wtotal=0
Amt_Dtotal=0
dataW =[]
dataD=[]
counts_W=defaultdict(int)
counts_D=defaultdict(int)
for row in reader:
if(row[28]=='W'):
counts_W[row[5]] += 1
Amt_Wtotal += float(row[6])
dataW.append(Amt_Wtotal)
else:
counts_D[row[5]] += 1
Amt_Dtotal += float(row[6])
dataD.append(Amt_Dtotal)
当我使用412KB的文件运行此代码时,不会出现错误,但当我使用1.8MB的文件运行时,会出现以下错误:
if(row[28]=='W'): IndexError: list index out of range
我的档案是这样的
标题
价值观
你能告诉我如何找到两个数据集之间的相关性,这是一个列表吗
for row in reader:
try:
if(row[28]=='W'):
counts_W[row[5]] += 1
Amt_Wtotal += float(row[6])
dataW.append(Amt_Wtotal)
else:
counts_D[row[5]] += 1
Amt_Dtotal += float(row[6])
dataD.append(Amt_Dtotal)
except:
#handle the exception here.
# "continue" will ignore it and move to the next item in the loop.
# I suspect what you actually want is to duplicate the "else" clause here.
看起来1.8MB文件中有一行字段比预期的少。这不是文件的大小,而是其中的一行或多行。对于给定数据集defaultdict(,{'2239': 1, '1700': 1, '2458': 1, '10016': 1, '2056': 1, '2376': 1, '1965': 1, '1974': 1, '2425': 1, '21768': 1, '2069': 1, '2404': 1, '2402': 1, '1763': 1, '1762': 1, '1910': 1, '1746': 1, '10036': 1}) [3480.0, 7080.0, 10440.0, 13200.0, 16800.0, 20400.0, 23880.0, 27480.0, 30840.0, 38040.0, 41520.0, 44880.0, 48480.0, 52080.0, 55680.0, 59280.0, 62520.0, 66120.0, 67580.0, 69620.0, 69621.0]count==21,即我已在单独的列表中获取了金额,我是否可以将tat附加到字典中,以及如何访问它。您的意思是这回答了您的问题,还是仍然存在一些问题?先生,请回答上述问题]
2,M,17748,60,60,21768,1460.0,7,2011-04-02 00:00:00,0,B,5,2011-07-22 03:03:00,52.0,1,1992,2011,2011,22,2,7,0,3,4,21768,1992-07-05 00:00:00,26,21768,W,50f38a469cf9c253d600000c,21768
1,M,18002,3,3,1746,3480.0,2,2011-04-07 00:00:00,0,B,5,2011-07-25 01:03:00,123.0,1,1985,2011,2011,25,7,7,0,1,4,1746,1985-02-05 00:00:00,3,1746,D,50f38a469cf9c253d600000d,1746
for row in reader:
try:
if(row[28]=='W'):
counts_W[row[5]] += 1
Amt_Wtotal += float(row[6])
dataW.append(Amt_Wtotal)
else:
counts_D[row[5]] += 1
Amt_Dtotal += float(row[6])
dataD.append(Amt_Dtotal)
except:
#handle the exception here.
# "continue" will ignore it and move to the next item in the loop.
# I suspect what you actually want is to duplicate the "else" clause here.