【问题标题】:iterating over a numpy array returns only the last item遍历 numpy 数组仅返回最后一项
【发布时间】:2021-08-07 23:38:53
【问题描述】:

我正在尝试使用正弦波函数进行导航。 我的主要代码中有两个函数:

def stop():
    steering = 1024
    throttle = 1024
    return steering, throttle

def case2():
    steering = []
    vector = np.vectorize(np.int)
    time_interval = 5
    samples = 50
    t = np.linspace(0, time_interval, samples)
    time_req = 5.0
    A = 900
    vec_steering = A*np.sin(2*(np.pi)*t/time_req) + 1024
    vec_steering = vector(vec_steering)
    throttle = 1050
    for steering in vec_steering:
        steering, throttle = steering, throttle
    return steering, throttle

函数stop()返回给我规定的油门、转向值,但函数case2()只返回给我:(1023, 1050)

'case2()' 函数的预期正弦输出是这样的:

(1024, 1050)
(1139, 1050)
(1252, 1050)
(1361, 1050)
(1465, 1050)
(1562, 1050)
(1650, 1050)
(1727, 1050)
(1793, 1050)
(1846, 1050)
(1886, 1050)
(1912, 1050)
(1923, 1050)
(1919, 1050)
(1901, 1050)
(1868, 1050)
(1821, 1050)
(1762, 1050)
(1690, 1050)
(1607, 1050)
(1514, 1050)
(1414, 1050)
(1307, 1050)
(1196, 1050)
(1081, 1050)
(966, 1050)
(851, 1050)
(740, 1050)
(633, 1050)
(533, 1050)
(440, 1050)
(357, 1050)
(285, 1050)
(226, 1050)
(179, 1050)
(146, 1050)
(128, 1050)
(124, 1050)
(135, 1050)
(161, 1050)
(201, 1050)
(254, 1050)
(320, 1050)
(397, 1050)
(485, 1050)
(582, 1050)
(686, 1050)
(795, 1050)
(908, 1050)
(1023, 1050)

谁能告诉我我哪里做错了?

【问题讨论】:

    标签: python numpy for-loop return trigonometry


    【解决方案1】:
    steering, throttle = steering, throttle
    

    这个语句实际上什么也没做。它将变量按原样重新分配给它们自己。

    此外,for 循环在返回任何内容之前进行完全评估,从而仅返回最后分配的 steeringthrottle 值。在这种情况下,这不会产生影响,因为循环中没有任何变化:

    for steering in vec_steering:
        steering, throttle = steering, throttle
    

    我不确定你到底想做什么,但我认为这更像你想要的:

    def case2():
        steering = []
        vector = np.vectorize(np.int)
        time_interval = 5
        samples = 50
        t = np.linspace(0, time_interval, samples)
        time_req = 5.0
        A = 900
        vec_steering = A*np.sin(2*(np.pi)*t/time_req) + 1024
        vec_steering = vector(vec_steering)
        throttle = 1050
        return [(steering, throttle,) for steering in vec_steering]
    

    我也不知道这是否是实现你想要的好方法,不知道你到底想做什么,但希望这会有所帮助。

    但是请注意,这现在返回一个不使用 numpys 向量化的元组列表。你也可以这样做:

    def case2(samples: int = 50,
            time_interval: int = 5,
            time_req: float = 5.0,
            steering_ampl: int = 900,
            throttle_ampl: int = 1050,
            steering_offset: int = 1024):
        
        vector = np.vectorize(np.int)
        t = np.linspace(0, time_interval, samples)
        vec_steering = vector(
            steering_ampl*np.sin(2*(np.pi)*t/time_req) + steering_offset
            )
        throttle_vec = throttle_ampl * np.ones(vec_steering.size,
                                 dtype=np.int)
        return np.vstack([throttle_vec, vec_steering])
    

    我还将一些常量移到这里作为参数并包含类型提示。 steering 列表也被删除,因为它不需要。

    最后我清理了一下。然而,什么需要“干净”可能有点主观。后一个示例中使用的变量名称是否是一种改进,在某种程度上也是一个见仁见智的问题。

    【讨论】:

    • 感谢您的时间伙伴。我在这里要做的是返回一个转向和油门值列表,让机器人遵循正弦曲线模式。我的油门值是固定的(恒定速度),但转向值不断变化。 return [(steering, throttle,) 用于在 vec_steering 中转向] 这行没有返回任何东西。我基本上希望我的输出是预期的正弦输出,如上所示。
    • @siddharth 完全没问题;我也从回答问题中学习。你在用微蟒吗? “返回 [(转向,油门,)用于在 vec_steering 中转向] 这条线没有返回任何东西” 嗯,对我来说它有效。您是否有可能将所有相关代码放在 github 存储库或其他东西中?我用我的确切代码做了一个要点here。对我来说,它打印 [(1050, 1024), (1050, 1139), ... (stuff left out bc character limit) , (1050, 908), (1050, 1023)]
    【解决方案2】:

    您的最后一个 return 语句在 for 循环之外,因此它只返回一个元组,但如果您想要所有值,那么您可以将要返回的值存储在一个列表中,然后在循环后返回该列表。在这里,您只是更新转向和油门的值,而不是将其存储在任何地方。

    【讨论】:

    • 您的意思是将每个元素从 for 循环附加到列表并返回?
    • @siddharth 是的。您可以简单地追加到列表中,也可以使用列表推导来追加元素,然后在循环后返回。这里 ans 是 vec_steering 中的转向列表: ans.append((steering,throttle)) return ans
    • 谢谢它的工作,但如果我想把这个ans解包为(转向、油门)并返回它呢?
    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2014-11-11
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多