我是 Python 的新手,遇到了一堆问题,我需要一些帮助。我有一个 python 函数需要解析一些简单的 0 和 1 值。
111000111
111000111
101111111
101100001
111111111
我需要用二维数组存储每个 0,以便稍后引用这些位置。但是我得到的索引超出范围,我做错了什么以及如何解决它?
这是python代码:
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;
最佳答案
当你有 subset[subSetCount]
时,subset 仍然是一个空列表,所以索引超出范围。 rowList[rowCount]
和 rowList[columnCount]
也是如此。
从这里开始,我将稍微推测一下您正在尝试做些什么来帮助您修复它。似乎也许而不是
subset[subSetCount].append(rowList[rowCount]);
subset[subSetCount].append(rowList[columnCount]);
你只是想要
rowList.append( rowCount )
columnList.append( columnCount )
然后,在 for columnCount
循环之后,也许您想要
subset.append( [rowList, columnList] )
或类似的东西。
关于Python,列出二维数组中超出范围的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12065019/