2d array out of bounds python

时间:2015-11-12 11:49:11

标签: python arrays axis

I am trying to put values between two 2d string arrays. atts is a dictionariy, have the correct values (I checked). I have a loop, where the first and second interations work as expected, copying the arrays, but the third iteration (where key is '3rd' and its value is 301 which is bigger than the other two keys) doesn't work. it actually copies all as expected, but gives me a warning "index 301 is out of bounds for axis 0 with size 301". it's really confusing, since the loop does the same for every key so why won't it work only for the third iteration?

for key in atts:

        cutTable = array(range( (atts[key]) * (len(Table[0])-1)), dtype='S10').reshape( (atts[key]),(len(Table[0])-1))
        m=0
        for i in range(len(Table)):                      
            if(Table[i][index_to_cut] == key):
                m+=1
                beenHere= False
                for k in range(len(Table[0])):
                     if (k==index_to_cut):
                       beenHere = True
                       continue
                     else:
                         if(beenHere):
                             cutTable[m-1][k-1] = Table[i][k]
                         else:
                             cutTable[m-1][k] = Table[i][k]

I printed dict atts for youall:

atts: { '2nd': 101, '1st':111, '3rd':301, 'crew':547)

I'm kinda stuck and I don't know how to solve this, if anyone knows I will be grateful..

1 个答案:

答案 0 :(得分:0)

您没有正确使用Numpy。 Numpy的优势在于避免Python代码中的慢循环,并用C,Fortran或C ++编写的Numpy结构的内部循环替换它们。

即使在纯Python中,for index in range(len(sequence))只是使用index来编制索引sequence反模式,因为您可以迭代{{1}的元素直接。例如,变量sequence是不必要的。除了索引处的值之外还需要索引时,还有i函数。

增量enumerate()不是最理想的。如果移动到m分支的末尾,则无需减去一个用于在if中存储值。

Pure Python看起来像这样(cut_tablenp模块):

numpy

但是假设for key, value in atts.iteritems(): cut_table = np.zeros((value, len(table[0] - 1)), dtype='S10') j = 0 for row in table: if row[index_to_cut] == key: for j, value in enumerate(row): cut_table[j][j - (j > index_to_cut)] = value j += 1 assert j == value 数组与cut_table中的key值一样长,你应该真正使用Numpy来解决这个问题,这可能是这样的:

table

这将从表中选择列for key, value in atts.iteritems(): cut_table = np.delete( table[table[:, index_to_cut] == key], index_to_cut ) assert len(cut_table) == value 中的值等于index_to_cut的所有行,并删除该列。没有Python代码中的任何慢循环。