我已经阅读了多个询问错误的问题:
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定义为:
时,长度为266的Chosen_Columns_best_result不能适合x [i + 1]x = np.zeros((n+1,len(Init_Sequence)))
希望任何人都能向我(非常初学者)解释如何解决这个问题。
答案 0 :(得分:0)
IndexError
的原因是您使用变量名称i
两次(for i in range(n)
和for i in range(rand-1,rand)
)。在调用x[i+1]
时,我假设您期望来自外部i
循环的for
,但其值会在内部意外更改。尝试为另一个for
循环使用不同的索引变量名称。