【问题标题】:list.append() does not appear to work correctlylist.append() 似乎无法正常工作
【发布时间】:2016-02-11 10:45:42
【问题描述】:

我创建了一个 python 函数,它应该采用一系列 3D 坐标并将它们放在一个列表中(在一个列表中)。

但是当我打印出coord_list 时,它似乎没有正确附加,例如当输入这些坐标时:

[1,2,3]

[2,3,4]

[3,4,5]

coord_list 的最终输出(忽略“q”)将是:[[3,4,5],[3,4,5],[3,4,5]]

为什么它不能正确附加,如何解决?

def coords() :

    xyz_list = []
    coord_list = []

    while 1 :
        xyz = raw_input("Enter co-ordinates (x,y,z) enter 'q' when done: ")
        xyz = str(xyz)

        del xyz_list[:]

        for num in xyz.split(","):
            xyz_list.append(num)

        print xyz_list

        if xyz[0] != 'q' :
            coord_list.append(xyz_list)
            print coord_list
        else :
            break

coords()

【问题讨论】:

  • 我测试了你的脚本,没问题。
  • 您能否更具体地说明“为什么不能正确附加”的意思?你认为它应该做什么?

标签: python list function append coordinates


【解决方案1】:

这是因为coord_list 正在存储[xyz_list, xyz_list, ...]。您在每次迭代中更新xyz_list,而coord_list 又会更新。

【讨论】:

    【解决方案2】:

    问题在于访问托管堆的del。新对象(xyz_list 的成员)出现在同一位置,因为包含列表没有被删除。因此列表成员就地替换了以前的成员,coord_list 中的引用将指向新值。

    python 2.7.9 (Linux) 中的复制:

    $ python coords.py
    
    Enter co-ordinates (x,y,z) enter 'q' when done: 1,2,3
    ['1', '2', '3']
    [['1', '2', '3']]
    Enter co-ordinates (x,y,z) enter 'q' when done: 2,3,4
    ['2', '3', '4']
    [['2', '3', '4'], ['2', '3', '4']]
    Enter co-ordinates (x,y,z) enter 'q' when done: 3,4,5
    ['3', '4', '5']
    [['3', '4', '5'], ['3', '4', '5'], ['3', '4', '5']]
    

    我对脚本做了一个小改动:del xyz_list[:] --> xyz_list = []

    现在可以了:

    $ python coords.py
    
    Enter co-ordinates (x,y,z) enter 'q' when done: 1,2,3
    ['1', '2', '3']
    [['1', '2', '3']]
    Enter co-ordinates (x,y,z) enter 'q' when done: 2,3,4
    ['2', '3', '4']
    [['1', '2', '3'], ['2', '3', '4']]
    Enter co-ordinates (x,y,z) enter 'q' when done: 3,4,5
    ['3', '4', '5']
    [['1', '2', '3'], ['2', '3', '4'], ['3', '4', '5']]
    

    【讨论】:

    • 谢谢,一个简单的错误有时会让你发疯
    【解决方案3】:

    删除del并将xyz_list添加到coord_list后清除列表:

    def coords() :
    
        xyz_list = []
        coord_list = []
    
        while 1 :
            xyz = raw_input("Enter co-ordinates (x,y,z) enter 'q' when done: ")
            xyz = str(xyz)
    
            for num in xyz.split(","):
                xyz_list.append(num)
    
            print xyz_list
    
            if xyz[0] != 'q' :
                coord_list.append(xyz_list)
                print coord_list
                xyz_list = []
            else :
                break
    
    coords()
    

    输出:

    Enter co-ordinates (x,y,z) enter 'q' when done: 1,2,3
    ['1', '2', '3']
    [['1', '2', '3']]
    Enter co-ordinates (x,y,z) enter 'q' when done: 4,5,6
    ['4', '5', '6']
    [['1', '2', '3'], ['4', '5', '6']]
    

    【讨论】:

      【解决方案4】:

      替换以下表达式:

      del xyz_list[:]
      

      与:

      xyz_list = []
      

      【讨论】:

        猜你喜欢
        • 2017-09-04
        • 2012-05-06
        • 2013-02-17
        • 2017-10-09
        • 2017-08-03
        • 2014-05-02
        • 2011-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多