chrisis
chrisis

Reputation: 119

TypeError: list indices must be integers or slices, not tuple?

I want to devide each element of a matrix for the elements of a vector. It looks a really easy operation but I get the following error:

TypeError: list indices must be integers or slices, not tuple

How to solve this problem? Here below you can find the script with other information. Thanks again for the help.

I tried to plot variables with whos. Apparently, I have list. I do not know exctly the difference between list and vectors.

enter code here
##-- DATA
Addm_strength=[7, 8 ,9 ,10]
stress= [[1, 4], 
   [-5, -8],
   [ 4, 8 ] ,
   [ 4, 8 ] ]
hef_sigma=[0.005, 0.006]

ratio_lam = np.zeros( (len(Addm_strength), len(hef_sigma)) ) 
print('ratio_lam',ratio_lam)

#-- CODE
for i in range(0, len(Addm_strength)):
    for j in range(0, len(hef_sigma)):
        ratio_lam[i,j]=stress[i,j]h/Addm_strengt[i]

print('ratio_lam',ratio_lam)

The expected result is a matrix called ratio_lam.

Upvotes: 1

Views: 10726

Answers (1)

Stephen Meschke
Stephen Meschke

Reputation: 2940

You need to change:

stress= [[1, 4], 
   [-5, -8],
   [ 4, 8 ] ,
   [ 4, 8 ] ]

To:

stress= np.array([[1, 4], 
   [-5, -8],
   [ 4, 8 ] ,
   [ 4, 8 ] ])

This is the output:

ratio_lam [[ 0.14285714  0.57142857]
 [-0.625      -1.        ]
 [ 0.44444444  0.88888889]
 [ 0.4         0.8       ]]

In the line ratio_lam[i,j]=stress[i,j]/Addm_strength[i], stress is accessed as an element in an array. Stress needs to be an array, not a list.

Upvotes: 4

Related Questions