【问题标题】:indexing on float values in coordinates索引坐标中的浮点值
【发布时间】:2022-11-17 13:14:54
【问题描述】:

我有一个清单

points = [[6033.02, -24791.2], [7008.29, -24257.0], [7128.66, -23434.0], [7235.19, -22899.3], [6590.0, -22308.7]]

和

centre = [37621.265, -32837.66499999999]

我想用这个函数翻译点的值

def translate(center, points):
    new_points_x = []
    new_points_y = []
    new_points = []
    for i in points:
        # print(str(i[0]))
        new_points_x.append(i[0]-center[0]) 
    
    for i in points:
        new_points_y.append(i[1]-center[1])

    for i in range(0,len(new_points_x)):
        new_points.append([new_points_x[i], new_points_y[i]])

    return new_points 

它给我错误

TypeError: 'float' object is not subscriptable

我为浮动索引尝试了很多东西但都失败了。

【问题讨论】:

  • 你有什么问题?请阅读How to Ask。如果您正在寻找调试帮助,您需要制作一个 minimal reproducible example,包括完整但最少的代码、预期输出和 full error message with traceback。当我尝试 translate(centre, points) 时,我没有得到那个错误。也许你传递了一些错误的东西,比如你把论点弄反了?

标签: python python-3.x


【解决方案1】:

看起来您以错误的顺序将 center 和 points 传递给函数。这样做会导致点成为列表而不是列表列表,并且 i 将是浮点数而不是列表。

尝试 -

points = [[6033.02, -24791.2], [7008.29, -24257.0], [7128.66, -23434.0], [7235.19, -22899.3], [6590.0, -22308.7]]
centre = [37621.265, -32837.66499999999]

def translate(center, points):
    new_points_x = []
    new_points_y = []
    new_points = []
    for i in points:
        new_points_x.append(i[0]-center[0]) 
    
    for i in points:
        new_points_y.append(i[1]-center[1])

    for i in range(0,len(new_points_x)):
        new_points.append([new_points_x[i], new_points_y[i]])

    return new_points

print(translate(centre, points))

输出:

[[-31588.245, 8046.464999999993], [-30612.975, 8580.664999999994], [-30492.605, 9403.664999999994], [-30386.075, 9938.364999999994], [-31031.265, 10528.964999999993]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多