Python,列出二维数组中超出范围的索引

标签 python list

我是 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/

相关文章:

python - 我可以在 django 模板中渲染后添加或更新新变量吗

Python:可以支持多处理的 hyperopt 的替代方案?

Python的列表对象不允许索引值改变

linq - LinQ 中的动态列名

Python:从一堆 "key: value"字符串创建字典?

python - 如何在两个数据框中查找匹配项

Python 3 托管

python - 在 Windows 中安装 LabelImg Annotation 工具

Python 将列表转换为每个键值为 1 的字典

javascript - 如何循环浏览输入的 Viewbag 列表?