numpy “int”对象不可为R3订阅

dly7yett  于 2022-12-26  发布在  其他
关注(0)|答案(1)|浏览(133)

我创建了一个for循环来计算R3,如下面的代码,但是,有一个错误。错误是'int'对象是不可subcriptable。我检查了存储的大小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
xuo3flqw

xuo3flqw1#

a是一个integer;
在这一行中可能是:

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

它应该是storage_a而不是a

相关问题