简体   繁体   English

TypeError:列表索引必须是整数或切片,而不是元组?

[英]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 TypeError:列表索引必须是整数或切片,而不是元组

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. 我试图用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. 预期结果是一个称为ratio_lam的矩阵。

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. ratio_lam[i,j]=stress[i,j]/Addm_strength[i] ,将应力作为数组中的元素进行访问。 Stress needs to be an array, not a list. 重音需要是数组,而不是列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:列表索引必须是整数或切片,而不是元组 - TypeError: list indices must be integers or slices, not tuple 列表类型错误:列表索引必须是整数或切片,而不是元组 - List of lists TypeError: list indices must be integers or slices, not tuple TypeError:列表索引必须是整数或切片,而不是元组列表的元组 - TypeError: list indices must be integers or slices, not tuple for list of tuples 类型错误:列表索引必须是整数或切片,而不是在 python 中使用 sys 导入的元组 - TypeError: list indices must be integers or slices, not tuple with sys import in python 新编码器:TypeError:列表索引必须是整数或切片,而不是元组 - New coder: TypeError: list indices must be integers or slices, not tuple Python棋盘游戏-“类型错误:列表索引必须是整数或切片,而不是元组” - Python Board Game - "TypeError: list indices must be integers or slices, not tuple" TypeError:列表索引必须是整数或切片,而不是电影分级数据的元组 - TypeError: list indices must be integers or slices, not tuple for Movie Rating Data Python 类型错误:列表索引必须是整数或切片,而不是元组 - Python TypeError: list indices must be integers or slices, not tuple 迭代字典会抛出 TypeError:列表索引必须是整数或切片,而不是元组 - Iterating on dictionary throws TypeError: list indices must be integers or slices, not tuple 如何修复“类型错误:列表索引必须是整数或切片,而不是元组” - How to fix "TypeError: list indices must be integers or slices, not tuple"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM