1

I'm new to Python and running into a heap of problems and I need some help. I have a python function that needs to parse through some simple values with 0s and 1s.

 111000111
 111000111
 101111111
 101100001
 111111111

I need to store each 0s with a 2D array so the positions can be referenced later. But I'm getting a index out of range, what am I doing wrong and how to fix it?

Here's the python code:

def storeSubset(fileName):
locale = dataParser(fileName); test = [];
subset = []; subSetCount = 0; columnCt =0;
rowList = []; columnList=[];
for rowCount in range(0, len(locale)):
#   print " "; print " "
#   print "Values of one row locale[row]: ", locale[rowCount]; 
#   print "We are at row# 'row': ", rowCount;
#   print "Length of row is int(len(locale[rowCount]))", int(len(locale[rowCount]));
    test = locale[rowCount];

    for columnCount in range (0, int(len(locale[rowCount])-1)):
        rowVal = locale[rowCount][columnCount];
#       print "Value of column is :", rowVal;
        if (rowVal==0):
#           print "columnVal = 0, the column position is ", columnCount;
            subset[subSetCount].append(rowList[rowCount]);
            subset[subSetCount].append(rowList[columnCount]);
subSetCount+=1;
print "The Subsets is :", subset;
return subset;
1
  • 1
    @David has the answer covered, but as a side note (that will save you some typing!) - you don't have to use semicolons to end a line in Python. Also, take a look at the docs on looping techniques (docs.python.org/tutorial/datastructures.html#tut-loopidioms) - you can actually use syntax like for rowCount in locale: to iterate over each item in locale (so then you would have test = rowCount, etc.). Commented Aug 22, 2012 at 1:07

3 Answers 3

3

When you have subset[subSetCount], subset is still an empty list, so the index is out of range. The same is true for rowList[rowCount] and rowList[columnCount].

From here, I'll speculate a little about what you're trying to do to help you fix it. It seems like maybe instead of

subset[subSetCount].append(rowList[rowCount]);
subset[subSetCount].append(rowList[columnCount]);

you just want

rowList.append( rowCount )
columnList.append( columnCount )

Then, after the for columnCount loop, maybe you want

subset.append( [rowList, columnList] )

or something like that.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not 100% certain exactly what you're trying to do, so I'm just stabbing in the dark here.

subset[subSetCount].append(rowList[rowCount]);
subset[subSetCount].append(rowList[columnCount]);

This seems problematic. You're appending to an index, but I don't see how this index has anything in it. I'm assuming this is the issue. Perhaps just

subset.append(rowList[rowCount]) 

would accomplish what you what.

Also, you don't need semicolons =D

Comments

1

This is an occasion where numpy would be useful:

import numpy as np
with open(datafile) as f:
     lines = [list(l.strip()) for l in f]

array = np.array(lines)
zeros = array == 0
ones = array == 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.