首页 > 解决方案 > 如何在每次循环后重置列表?

问题描述

每次调用 while 循环时,我都想创建一个新列表(self.Input/self.Output)。并将这个新列表附加到另一个列表的末尾(self.Input_full/self.Output_full)。

我试图在 while 循环开始时重置列表,只需将它们设置回空:self.Output = [[],[],[],[]] 或删除它们持有的信息:del self .Output[:] 但这不起作用,从那以后我在完整列表中得到了空列表

import threading

class PepperCommandEvaluator(object):

    def __init__(self):
        self.Input = [[],[],[],[]]
        self.Input_full = []
        self.Output = [[],[],[],[]]
        self.Output_full = []
        self.count = 0
        self.event = threading.Event()

    def send_thread(self):
        while self.count < 2:
            self.count = self.count + 1
            self.event.set()      
            sequence = [[1,1,1],[1,0,1],[1,3,3]]
            for cmd in sequence:
                rospy.loginfo("sending command")
                rospy.Rate(0.5).sleep()
                msg = Twist()
                msg.linear.x = cmd[0]
                msg.linear.y = cmd[1]
                msg.angular.z = cmd[2]
                t = rospy.get_rostime()
                self.Input[0].append(cmd[0])
                self.Input[1].append(cmd[1])
                self.Input[2].append(cmd[2])
                self.Input[3].append(t.secs + t.nsecs * 1e-9)
            self.Input_full.append(self.Input)
            self.event.clear()

    def receive_thread(self,msg):
        if self.event.isSet():
            self.frame_id = msg.header.frame_id
            self.x_odom = msg.pose.pose.position.x
            self.y_odom = msg.pose.pose.position.y
            self.z_odom = msg.pose.pose.position.z
            self.ang_odom = msg.pose.pose.orientation.z
            self.time = msg.header.stamp.secs
            self.Output[0].append(self.x_odom)
            self.Output[1].append(self.y_odom)
            self.Output[2].append(self.ang_odom)
            self.Output[3].append(self.time)
        else:
            self.Output_full.append(self.Output)
if __name__ == "__main__":
    tros = PepperCommandEvaluator()
    tros.send_thread()

我想要的输出是在每个循环中获得一个新的 self.Input 和 self.Output_odom,并将这个列表分别附加到 self.Input_full 和 self.Output_full_odom 中。最后取决于循环运行的次数,这应该如下所示: self.Output_full = [[self.Output_1,self.Output_2,...,self.Output_n]]

标签: pythonlistloops

解决方案


由于 python 中的列表是通过引用处理的,所以当您将引用附加InputInput_full,然后删除Input时,您也会删除Input_full. 为避免这种情况,您要附加 的副本Input,然后清除真实的东西。您还可以Input在附加后将引用更改为空列表。

    def send_thread(self):
    while self.count < 2:
        self.count = self.count + 1
        self.event.set()      
        sequence = [[1,1,1],[1,0,1],[1,3,3]]
        self.Input = [[],[],[]] # Reassigning to a new list at the top
        for cmd in sequence:
            rospy.loginfo("sending command")
            rospy.Rate(0.5).sleep()
            msg = Twist()
            msg.linear.x = cmd[0]
            msg.linear.y = cmd[1]
            msg.angular.z = cmd[2]
            t = rospy.get_rostime()
            self.Input[0].append(cmd[0])
            self.Input[1].append(cmd[1])
            self.Input[2].append(cmd[2])
            self.Input[3].append(t.secs + t.nsecs * 1e-9)
        self.Input_full.append(self.Input)
        self.event.clear()

推荐阅读