【发布时间】:2022-01-19 17:01:50
【问题描述】:
我正在尝试创建一系列作业,并将它们放入一个数组中。
如果我单独运行这些行,编码就可以工作。一个问题是当count 等于amountofmachines 时它不会停止while 循环
它给出了错误:
IndexError: list assignment index out of range
我对 python 有点陌生,习惯于 Matlab。我怎样才能结束这个while循环并使代码在a.sort()行恢复?
随机导入
将 numpy 导入为 np
从随机导入randint
MachineNumber = 6 #amount of machines imported from Anylogic
JobNumber = 4 #amount of job sequences
JobSeqList = np.zeros((JobNumber,MachineNumber), dtype=np.int64)
amountofmachines = randint(1, MachineNumber) #dictated how much machines the order goes through
a = [0]*amountofmachines #initialize array of machines sequence
count = 0 #initialize array list of machines
element = [n for n in range(1, MachineNumber+1)]
while count <= amountofmachines:
a[count] = random.choice(element)
element.remove(a[count])
count = count + 1
a.sort() #sorts the randomized sequence
A = np.asarray(a) #make an array of the list
A = np.pad(A, (0,MachineNumber-len(a)), 'constant') #adds zeros to the end of sequence
#add the sequence to the array of all sequences
JobSeqList[0,:] = A[:]
【问题讨论】:
-
请正确缩进代码