"int"对象对于 R3 不可下标



我创建了一个for循环来计算R3,如下面的代码所示,但是,有一个错误。错误是'int'对象不可下标。我检查了存储空间的大小a (np.shape(a) =(4,5)。因此for循环是:idn从0到3,idj从0到4,这也使得storage_R3 =(4,5)的形状。谁能告诉我该怎么做?

import numpy as np
import random
n = 4
w = 5
v = 3
low = 0
high = 500
sample_size = 5
def get_numbers(low, high, sample_size):
return random.sample(range(low, high), sample_size)
p_one = np.array(get_numbers(low, high, sample_size), dtype = int)
p_two = np.array(get_numbers(low, high, sample_size), dtype = int)
p_three = np.array(get_numbers(low, high, sample_size), dtype = int)
p_four = np.array(get_numbers(low, high, sample_size), dtype = int)

c_one = np.array(get_numbers(low, high, sample_size), dtype = int)
c_two = np.array(get_numbers(low, high, sample_size), dtype = int)
c_three = np.array(get_numbers(low, high, sample_size), dtype = int)
c_four = np.array(get_numbers(low, high, sample_size), dtype = int)
#Define a for SWAP
storage_a = []
for i in range(0,n):
storage_atemp = []
for j in range(0,sample_size):
if p[i][j] >= c[i][j]:
a = 1
else:
a = 0 
storage_atemp.append(a)
storage_a.append(storage_atemp)
np.shape(storage_a)
for idn in range(0,n): #------------------n-1 for the last process step, idn refers to the process steps

p = [p_one, p_two, p_three, p_four]
c = [c_one, c_two, c_three, c_four]
storage_R3 = []
for idn in range(0,n):
for idj in range(0,sample_size):
R3 = a[idn][idj] * p[idn][idj] + (1-a[idn][idj])* c[idn][idj] + v + 2*w
storage_R3.append(R3)
TypeError                                 Traceback (most recent call last)
<ipython-input-11-6dfea217881a> in <module>
6 for idn in range(0,n):
7    for idj in range(0,sample_size):
----> 8         R3 = a[idn][idj] * p[idn][idj] + (1-a[idn][idj])* c[idn][idj] + v + 2*w
TypeError: 'int' object is not subscriptable 

ainteger;

这一行可以是:

R3 = storage_a[idn][idj] * p[idn][idj] + (1-a[idn][idj])* c[idn][idj] + v + 2*w

应该是storage_a而不是a

最新更新