IndexError:索引超出范围

差不多

我已阅读有关该错误的多个问题:

IndexError: index 101 is out of bounds for axis 0 with size 11.我已经在第一次迭代中收到此错误。

IndexError                                Traceback (most recent call last)
<ipython-input-62-9ee6ac0ee12b> in <module>()

-->      x[i+1] = Chosen_Columns_best_result

问题是我似乎仍然无法理解问题所在。

我得到以下脚本:

import numpy as np
Init_Sepquence = [122, 172, 123, 175, 227, 177, 152, 129, 89, 87, 127, 150, 174, 147, 148, 201, 171, 222, 241, 203, 205, 206, 176, 125, 173, 226, 245, 230, 229, 180, 182, 134, 110, 92, 56, 58, 93, 135, 161, 212, 183, 133, 132, 156, 209, 157, 159, 109, 91, 112, 114, 95, 57, 59, 44, 60, 79, 119, 120, 118, 117, 163, 115, 96, 137, 186, 138, 97, 98, 80, 46, 78, 61, 81, 82, 63, 64, 84, 48, 32, 45, 33, 31, 47, 22, 50, 34, 23, 21, 49, 35, 65, 51, 39, 25, 38, 52, 37, 36, 68, 69, 85, 53, 29, 12, 11, 27, 9, 5, 7, 3, 13, 20, 4, 2, 6, 19, 28, 41, 54, 40, 18, 26, 24, 8, 16, 15, 14, 260, 238, 240, 254, 224, 252, 242, 261, 253, 225, 244, 255, 262, 243, 228, 207, 179, 128, 103, 124, 126, 153, 151, 149, 202, 204, 178, 130, 108, 154, 104, 106, 155, 208, 181, 210, 184, 213, 158, 131, 90, 72, 70, 107, 71, 55, 73, 113, 162, 160, 185, 187, 139, 116, 141, 101, 62, 99, 140, 100, 83, 66, 102, 67, 211, 250, 258, 266, 251, 223, 200, 198, 220, 168, 142, 144, 196, 234, 195, 167, 165, 216, 233, 246, 236, 235, 197, 146, 199, 145, 143, 170, 169, 221, 219, 217, 248, 249, 239, 259, 237, 256, 264, 257, 247, 263, 265, 111, 136, 94, 74, 75, 43, 76, 77, 42, 30, 231, 191, 193, 166, 192, 164, 190, 214, 189, 188, 86, 105, 88, 121, 10, 17, 194, 215, 232, 1, 218]
TotalNumbcol = 266
x = np.zeros((n+1,len(Init_Sequence)))                           # x = list with all best solutions
x[0] = Init_Sequence

n = 10
m = 10

for i in range(n):
    print i
    print 'Cycle:' + str(i)
    for j in range(m):
        # Creating a new random sequence
        Column_Numbers2 = list(np.arange(1,TotalNumbcol+1,1))
        Chosen_Columns2 = []
        shuffle(Column_Numbers2)
        rand = Column_Numbers2.pop()
        Chosen_Columns2.append(rand)
        while Column_Numbers2:
            for i in range(rand-1,rand):
                rand = NeighborsperColumn[i]
                rand = [y for y in rand if y not in Chosen_Columns2]
                shuffle(rand)
                try:
                    rand = rand.pop()
                    Chosen_Columns2.append(rand)
                    Column_Numbers2.remove(rand)
                except IndexError:
                    rand = Column_Numbers2.pop()
                    Chosen_Columns2.append(rand)
        Chosen_Columns_best_result = Chosen_Columns2 # In my complete script not every Chosen_Columns2 result is a 
                                                     #Chosen_Columns_best_result, but its to make this script a bit faster to read
    x[i+1] = Chosen_Columns_best_result # Here I try to change the i with zeros into Chosen_Columns_best_result, but it fails

我不明白为什么将x定义为x时,​​长度为266的Chosen_Columns_best_result无法容纳在x [i + 1]中:

x = np.zeros((n+1,len(Init_Sequence)))

希望任何人都可以向我(非常初级)解释如何解决此问题。

胜利者

的原因IndexError是您使用了i两次变量名for i in range(n)for i in range(rand-1,rand))。调用时x[i+1],我假设您期望i外部for循环提供,但是其值在内部意外更改。尝试为另一个for循环使用其他索引变量名称

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章