So I'm trying to create an array of the same size as [A]. What I want is a for-loop that checks if the 1st value of the ith element in the Array == 0, However it keeps telling me that there is more than one element in the truth value of an array when there shouldn't be as I indexed the 1st value of the ith element in my array. Here is my code:
n = 4
N = [i for i in range(1,n 1)]
V = [0] N
K = N [5]
M = [0,1]
A = np.array([(i,j,k) for i in V for j in K for k in M if i!=j])
C=[]
for i in A:
if A[i][0]==0:
C.append([0.7])
elif abs(A[i][0]-A[i][1])<=1:
C.append([1])
else:
C.append([0])
CodePudding user response:
When you go through your for loop, i is already each list in A, you can check this with the below:
for i in A:
print(i)
Which returns:
[0 1 0]
[0 1 1]
[0 2 0]
[0 2 1]...
So then calling A[i][0]
gives an array each time rather than an integer, so the comparison A[i][0] == 0
is not possible. To fix your problem, either do the below, which will change your i to get an index for every element in A:
for i in range(len(A)):
if A[i][0]==0:
C.append([0.7])
elif abs(A[i][0]-A[i][1])<=1:
C.append([1])
else:
C.append([0])
Or change all instances of A[i][x]
to i[x]
, and use the each list element of A that way, as follows:
for i in A:
if i[0]==0:
C.append([0.7])
elif abs(i[0]-i[1])<=1:
C.append([1])
else:
C.append([0])