首页 > 解决方案 > 如何在 Python 中将所有对象添加到列表中而不在 for 循环之前使用空列表?

问题描述

我想使用追加或扩展对象来列出而不使用 for 循环之前的空列表。

我想知道是否有任何函数或方法实现它,以便它可以将它们附加到列表中,然后移动代码中的下一行。

当我使用空列表时,我的问题是我需要移动代码的其余行才能使用新列表。

这是输出:

[u'person_0']
[u'person_0', u'chair_0']
[u'person_0', u'chair_0', u'chair_1']
[u'person_0', u'chair_0', u'chair_1', u'book_0']
[u'person_0', u'chair_0', u'chair_1', u'book_0', u'bottle_0']

我只需要最后一个输出:

[u'person_0', u'chair_0', u'chair_1', u'book_0', u'bottle_0']

这是我的代码的一部分:

                    my_tf_id = []
                    my_dis =[]
                    for obj_class in objects:

                        rospy.logdebug('Found ' + str(len(objects[obj_class])) + ' object(s) of type ' + obj_class)

                        for obj_type_index, coordinates in enumerate(objects[obj_class]):
#                     
                            rospy.logdebug('...' + obj_class + ' ' + str(obj_type_index) + ' at ' + str(coordinates))

                            ymin, xmin, ymax, xmax = coordinates
                            y_center = ymax - ((ymax - ymin) / 2)
                            x_center = xmax - ((xmax - xmin) / 2)

                            detected_object = DetectedObject()
                            detected_object.type.data = obj_class
                            detected_object.image_x.data = xmin
                            detected_object.image_y.data = ymin
                            detected_object.image_width.data = xmax - xmin
                            detected_object.image_height.data = ymax - ymin


                            publish_tf = False
                            if self._current_pc is None:
                                rospy.loginfo('No point cloud information available to track current object in scene')


                            else:

                                pc_list = list(pc2.read_points(self._current_pc, skip_nans=True, field_names=('x', 'y', 'z'), uvs=[(x_center, y_center)]))

                                if len(pc_list) > 0:

                                    publish_tf = True

                                    tf_id = obj_class + '_' + str(obj_type_index)        #object_number

                                    my_tf_id.append(tf_id)
                                    print my_tf_id

更新:

我用这个例子来简化它。

出于某种原因,我不需要移动其余代码行。

x = []
L = [2.0, 3, 5, 7, 11 , 9 , 10]
for i, j in enumerate(L) :
    x.append(j)
    print x

输出正常:

[2.0]
[2.0, 3]
[2.0, 3, 5]
[2.0, 3, 5, 7]
[2.0, 3, 5, 7, 11]
[2.0, 3, 5, 7, 11, 9]
[2.0, 3, 5, 7, 11, 9, 10]

当这条线print x向左移动时,输出如下,也是正常的:

[2.0, 3, 5, 7, 11, 9, 10]

我想将上一个示例的结构更改为此示例:

L = [2.0, 3, 5, 7, 11 , 9 , 10]

    for i, j in enumerate(L) :
        x = []
        x.append(j) #if any function or method gather them in this line, and moves to the next lines.
        print x

注意:
我不想修改这一行:for i, j in enumerate(L) :

请帮助我或提出任何建议。

先感谢您

标签: listpython-2.7append

解决方案


如果我正确理解了这个问题,听起来您正在寻找的是发电机。

所以而不是:

x = []
L = [2.0, 3, 5, 7, 11 , 9 , 10]
for i, j in enumerate(L) :
    x.append(j)

y = x[3]

您将改为使用:

L = [2.0, 3, 5, 7, 11 , 9 , 10]

def results(index):
    for i, j in enumerate(L) :
        if i == index:
            yield j

y = results(3)

y是一个生成器。您可以使用以下任一方法检索该值:

print(*y)

或者

print(next(y))

推荐阅读