【问题标题】:How to add all object to list in Python without using empty list before for loop?如何在 Python 中将所有对象添加到列表中而不在 for 循环之前使用空列表?
【发布时间】:2020-01-04 14:26:20
【问题描述】:

我想使用追加或扩展对象来列出而不使用 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) :

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

提前谢谢你

【问题讨论】:

  • 为什么要避免在循环之前创建空列表?即使您避免使用空列表,您也需要一些等效的数据结构来累积值。
  • 每个对象都有许多值(15 个值),我正在使用许多功能在线训练它,以便将某个人识别为机器人的目标。我需要使用最小、最大、....等比较它们的列表。当我使用空列表时,我需要添加所有值。但是没有空列表,它将需要一个人的索引。我认为,我不确定。

标签: list python-2.7 append


【解决方案1】:

如果我正确理解了这个问题,听起来您正在寻找的是一个生成器。

所以而不是:

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)

这里的ygenerator。您可以使用以下任一方法检索该值:

print(*y)

或者

print(next(y))

【讨论】:

    猜你喜欢
    • 2022-01-03
    • 2020-03-23
    • 2011-02-09
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2014-11-25
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多