【问题标题】:Conditional for loop which remembers the iteration number记住迭代次数的条件for循环
【发布时间】:2013-11-27 02:12:54
【问题描述】:

我正在尝试编写一个具有两个输入和一个输出的类。该类每秒调用 10 次。其中一个输入是一个不随时间变化的列表,而另一个输入是随时间变化的。

这个类的输出是输入列表的成员。 该类应如下所示。

import lib601.sm as sm

counter = 0
class FollowFigure(sm.SM):
    def __init__(self, waypointsList):
        self.waypoints = waypointsList
    def getNextValues(self, state, inp):
        global counter
        sensors = inp
        current_point = sensors.odometry.point() 

        if counter == 0:
            while not(self.waypoints[0].isNear(current_point, 0.01)):
                return (state, self.waypoints[0])
            counter += 1
        if counter == 1:
            while not(self.waypoints[1].isNear(current_point, 0.01)):
                return (state, self.waypoints[1])
            counter += 1
        if counter == 2:
            while not(self.waypoints[2].isNear(current_point, 0.01)):
                return (state, self.waypoints[2])
            counter += 1                
        if counter == 3:
            while not(self.waypoints[3].isNear(current_point, 0.01)):
                return (state, self.waypoints[3])

因此该类必须检查一个随时间变化的条件,如果该条件不满足,则返回 inp[0] 并等待它满足并且不检查其他条件。如果满足 inp1[0] 的条件,则转到下一个 while,并且在此类的下一次调用中,不要检查前一个 while。在过去的 5 个小时里,我一直在思考这个问题,但无法弄清楚如何解决它。它不应该那么难。可能我注意力不够集中。

我尝试为此编写一个 for 循环,但由于在某些情况下此类每秒调用 10 次,因此它没有应有的输出,因为每次调用迭代都会重置。顺便说一句,这个类应该适用于任何有限长度的 inp1。

编辑:上面的代码适用于具有 4 个元素的输入列表。

【问题讨论】:

  • 你需要回去重写你的整个方法来实现你的目标。你的代码是我见过的最糟糕的。您永远不会以程序化的方式调用一段时间。当您可以创建一个函数来执行一次时,不确定为什么要重复返回相同的值。底线是,你需要重写。
  • 保留已通过条件的项目的列表,然后在每次迭代时检查原始 inp1 中不在“已检查”列表中的项目。
  • 我真的不明白为什么while?如果条件不满足,程序将无限循环。
  • @RichardGrant 如果我知道如何编写该函数,我会编写它。
  • 请注意,所有这些while 语句实际上都是if 语句,因为里面有return 语句。所以它总是会在最多一次迭代中打破循环(如果while循环中的条件不满足,可以为0)。

标签: python iteration conditional


【解决方案1】:

您似乎正在编写一个应用程序,通过每 0.1 秒检查一次他们的位置来检测某人是否按顺序穿过规定的航路点。

在下面的代码中,我假设每次调用该函数时,它都会返回下一个要访问的检查点。所以一开始它会返回waypoint[0],稍后当我们到达waypoint[0]时,函数会返回waypoint[1],直到我们到达waypoint[2]

您的代码可以大大简化为:

import lib601.sm as sm

class FollowFigure(sm.SM):
    def __init__(self, waypointsList):
        self.waypoints = waypointsList
        self.next_to_visit = 0  # The next waypoint to be visited

    def getNextValues(self, state, inp):
        sensors = inp
        current_point = sensors.odometry.point()
        if self.waypoints[self.next_to_visit].isNear(current_point, 0.01):
            # We reached the next waypoint, go for next waypoint
            self.next_to_visit += 1
        # Return the next waypoint to be visited
        return (state, self.waypoints[self.next_to_visit])

请注意,函数内部没有while 循环。 return 语句将始终返回 waypoints[0],而我们还没有到达 waypoints[0],因为在我们到达 waypoints[0] 之前,self.next_to_visit 将始终为 0。因此,在任何函数调用中返回 waypoints[self.next_to_visit] 都会正确执行所需的任务。 =)

然后你可以在我们增加self.next_to_visit之后,通过检查self.next_to_visit的值是否等于waypoints列表的长度来检查你是否已经完成了所有的路标。如果相等,那么我们已经到了最终目的地,您可以相应地退回东西。

【讨论】:

  • 是的,这正是我想要的,而且它有效。谢谢。我想我不了解 OOP 中的很多东西。您已经在 init 方法中定义了 self.next_to_visit 属性。据我所知,每当调用一个类时,也会调用 init 方法,因此属性 self.next_to_visit 每次都会重置为零。我检查过它并没有重置为零,但我不明白为什么。嗯?
  • __init__ 方法只会在创建类的instance 时调用,而不是在调用类的method 时调用。你可以通过调用它的构造函数来创建一个实例,比如the_obj = FollowFigure(waypoints)。那是__init__ 被调用的时候。然后稍后,您的应用程序将每秒调用 方法getNextValues 10 次(如the_obj.getNextValues(state, inp))。由于它不调用构造函数FollowFigure(),因此不再调用方法__init__。所以你可以在那里进行任何初始化(这就是为什么它被称为__init__,用于初始化)
猜你喜欢
  • 1970-01-01
  • 2013-12-14
  • 1970-01-01
  • 2010-12-28
  • 2012-10-27
  • 2012-08-07
  • 2014-08-24
  • 2013-07-02
  • 1970-01-01
相关资源
最近更新 更多