arrays - Loop on list causes IndexError - Python -


i trying calculate area of file rle (run length encoding) format numbers using python. having trouble , if statements.

enter image description here

i trying calculate area of cells containing value 201.

my current error:

traceback (most recent call last):   file "d:\2016-2017\fall2016\spatialdatastructures\labs\lab4\area_calc_from_rle.py", line 68, in <module>     if cellvaluesasintlist[i] == 201: indexerror: string index out of range 

here file working with:

ncols         40 nrows         40 xllcorner     -2.3036649208516 yllcorner     -1.1518324594945 cellsize      25 nodata_value  -9999 13, 0, 1, 201, 26, 0, 1, 3, 12, 0, 1, 201, 39, 0, 1, 201, 39, 0, 1, 201, 39, 0, 2, 201, 33, 0, 4, 501, 2, 0, 2, 201, 32, 0, 4, 501, 3, 0, 3, 201, 29, 0, 5, 501, 5, 0, 2, 201, 26, 0, 7, 501, 6, 0, 1, 201, 25, 0, 8, 501, 6, 0, 1, 201, 25, 0, 7, 501, 7, 0, 1, 201, 25, 0, 6, 501, 8, 0, 1, 201, 25, 0, 6, 501, 8, 0, 1, 201, 6, 0, 4, 102, 15, 0, 7, 501, 7, 0, 1, 201, 6, 0, 6, 102, 14, 0, 7, 501, 5, 0, 2, 201, 6, 0, 6, 102, 15, 0, 6, 501, 5, 0, 1, 201, 7, 0, 5, 102, 16, 0, 6, 501, 5, 0, 1, 201, 7, 0, 4, 102, 18, 0, 8, 501, 2, 0, 5, 201, 3, 0, 3, 102, 19, 0, 9, 501, 5, 0, 2, 201, 25, 0, 8, 501, 6, 0, 1, 201, 29, 0, 4, 501, 6, 0, 1, 201, 29, 0, 4, 501, 6, 0, 1, 201, 9, 0, 1, 501, 5, 102, 15, 0, 2, 501, 7, 0, 1, 201, 9, 0, 6, 102, 23, 0, 2, 201, 8, 0, 1, 501, 6, 102, 3, 0, 4, 202, 15, 0, 2, 201, 4, 0, 6, 501, 6, 102, 2, 0, 2, 202, 2, 0, 8, 202, 5, 0, 4, 201, 4, 0, 8, 501, 4, 102, 3, 0, 1, 202, 5, 0, 2, 202, 3, 0, 6, 202, 1, 201, 7, 0, 8, 501, 2, 102, 1, 501, 21, 0, 1, 201, 6, 0, 11, 501, 22, 0, 1, 201, 6, 0, 8, 501, 25, 0, 1, 201, 6, 0, 8, 501, 25, 0, 1, 201, 1, 101, 5, 0, 8, 501, 25, 0, 1, 201, 1, 101, 5, 0, 8, 501, 14, 0, 3, 101, 8, 0, 1, 201, 1, 101, 5, 0, 11, 501, 12, 0, 4, 101, 6, 0, 2, 201, 1, 101, 4, 0, 12, 501, 11, 0, 4, 101, 6, 0, 1, 101, 1, 201, 1, 101, 4, 0, 12, 501, 21, 0, 1, 101, 1, 201, 5, 0, 11, 501, 23, 0, 1, 201, 5, 0, 8, 501, 26, 0, 1, 201, 5, 0, 8, 501, 26, 0, 1, 201, 5, 0, 7, 501, 27, 0, 1, 201, 5, 0, 8, 501, 8, 0 

this code:

fi = open(r'd:\2016-2017\fall2016\spatialdatastructures\labs\lab4\data\asrle.txt','r') filelines = fi.readlines() fi.close #---------------------------------------------------------------------  #--------------------------------------------------------------------- #populated variables required code filelines variable   cellvaluesasstring = [] linenum = 0 line in filelines: # each line filelines list   # number of cols, ncols, i.e. 1st line in file   if linenum == 0:     ncols = int(line[14:])   # number of rows, nrows, i.e. 2nd line in file   elif linenum == 1:     nrows = int(line[14:])   # cell size, cellsize, i.e. 5th line in file   elif linenum == 4:     cellsize = int(line[14:])   # cell values in rle format string, , i.e. 7th line in file   elif linenum == 6:     cellvaluesasstring = line   linenum = linenum + 1  # removes spaces cellvaluesasstring = cellvaluesasstring.replace(" ", "")  # convert string list of strings split comma cellvaluesasstringlist = cellvaluesasstring.split(',')  # convert strings integers cellvaluesasintlist = cellvaluesasstringlist index, item in enumerate(cellvaluesasstringlist):     cellvaluesasintlist[index] = int(cellvaluesasstringlist[index]) #---------------------------------------------------------------------  #this write code  #--------------------------------------------------------------------- cellcode = 201 codearea = 0  area = 0 npixels = 0 = 1  cellvaluesasintlist in line:   if cellvaluesasintlist[i] == 201:     b = cellvaluesasintlist[i-1]     npixels = npixels + b   else:     i=i+2     print npixles print "area: " + npixels * cellsize 

this looks great effort. issue running out of indices, raises indexerror. although situation requires each item index aware, luckily have poll iterated value.

for such situations, try tracking previous value:

npixels = 0 cells = cellvaluesasintlist                         # clarity  prev = none cell in cells:     if cell == 201:         npixels += prev                             # increment operator     prev = cell 

as rule of thumb, avoid mutating indices and iteration. while not direct problem, practice can cause many side effects. if possible, directly iterate sequence , work each iterated item. avoid mutating indices other means, e.g. reverse iteration, mutating copied sequence or use comprehensions.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -