【问题标题】:multiplication returning error (TypeError: 'int' object is not subscriptable)乘法返回错误(TypeError:'int'对象不可下标)
【发布时间】:2020-10-07 09:49:51
【问题描述】:

我是 python 的新手。谁能帮忙解决这个错误?

def distance_constraint(coords):
    cos = 0.099567846
    sin = 0.995030775
    outer = 2

    t = (coords[outer, 1] * cos) - (coords[outer, 0] * sin)
    return t

print(distance_constraint(5))

【问题讨论】:

标签: python-3.x


【解决方案1】:

您已将5 传递为coords,但5[outer,1] 没有任何意义。整数不能下标[]

coords 的值可能应该是至少三个坐标对的numpy.array

import numpy as np

def distance_constraint(coords):
    cos = 0.099567846
    sin = 0.995030775
    outer = 2

    t = (coords[outer, 1] * cos) - (coords[outer, 0] * sin)
    return t

coords = np.array([[1,2],[3,4],[5,6]])
print(distance_constraint(coords))  # outer,0 and outer,1 would refer to [5,6]

输出:

-4.3777467990000005

【讨论】:

    猜你喜欢
    • 2017-07-15
    • 2012-02-21
    • 2018-08-07
    • 2023-04-02
    • 2017-11-10
    • 2023-03-20
    • 2015-07-31
    相关资源
    最近更新 更多