【问题标题】:Add List to List in Python在 Python 中将列表添加到列表
【发布时间】:2016-01-09 02:29:17
【问题描述】:

我正面临一个我现在无法自行解决的问题。 它关于以下sn-p:

counter = 0
appendList = []
valueList = [[0], [0]]

for i in range(0,3):

    valueList[1] = counter
    print "Loop " , i  , " valueList: " , valueList
    print "Appending (valueList): " , valueList , " to (appendList): " , appendList
    appendList.append(valueList)
    counter = counter + 1


print "Final appendList: " , appendList

这会产生以下输出:

Loop  0  valueList:  [[0], 0]
Appending (valueList):  [[0], 0]  to (appendList):  []
Loop  1  valueList:  [[0], 1]
Appending (valueList):  [[0], 1]  to (appendList):  [[[0], 1]]
Loop  2  valueList:  [[0], 2]
Appending (valueList):  [[0], 2]  to (appendList):  [[[0], 2], [[0], 2]]
Final appendList:  [[[0], 2], [[0], 2], [[0], 2]]

我希望代码片段将不同的 List-Items 添加到 appendList。最终结果应该是这样的:

[[[0], 0], [[0], 1], [[0], 2]]

但是正如你所看到的,sn-p 用最高计数器的相同值填充appendList

有人可以向我解释这种行为或告诉我,我的错误在哪里吗?

【问题讨论】:

  • 每次分配一个副本,像这样appendList.append(valueList[:])
  • 谢谢伙计,这解决了我的问题。你能解释一下为什么吗?
  • 现在就写一个答案。

标签: python list loops for-loop append


【解决方案1】:

一个好习惯也是事后检查代码的效率:你定义了多个变量,而(对我来说)你对 appendList 感兴趣。

appendList = [ [[0], x] for x in range(3) ] # doStuff for index in range of n print appendList 使用列表推导,您可以将其减少为仅必要的计算。

【讨论】:

    【解决方案2】:

    你可以试试:

    counter = 0
    appendList = []
    valueList = [[0], [0]]
    
    for i in range(0,3):
    
        valueList[1] = counter
        print "Loop " , i  , " valueList: " , valueList
        print "Appending (valueList): " , valueList , " to (appendList): " , appendList
        appendList.append(valueList[:])
        counter = counter + 1
    
    
    print "Final appendList: " , appendList
    

    输出:

    Loop  0  valueList:  [[0], 0]
    Appending (valueList):  [[0], 0]  to (appendList):  []
    Loop  1  valueList:  [[0], 1]
    Appending (valueList):  [[0], 1]  to (appendList):  [[[0], 0]]
    Loop  2  valueList:  [[0], 2]
    Appending (valueList):  [[0], 2]  to (appendList):  [[[0], 0], [[0], 1]]
    Final appendList:  [[[0], 0], [[0], 1], [[0], 2]]
    

    说明:

    当您分配valuelist 对象时,所有元素都是从引用对象分配的。因此,您可以改为 b = a,而不是 b = a [:],因为它每次只从该对象复制。

    >>> a = [1, 2, 3]
    >>> b = a
    >>> a[:] = [4, 5, 6]
    >>> b
    [4, 5, 6]
    >>> a
    [4, 5, 6]
    >>> b = a [:]
    >>> a = [1, 2, 3]
    >>> b
    [4, 5, 6]
    >>> a
    [1, 2, 3]
    >>> 
    

    【讨论】:

    • 虽然它解决了问题,但这并不能解释原因,并且是@thefourtheye评论的精确副本。
    • 好吧,等一下。我正在写解释
    • @ppperry:现在可以了吗?
    【解决方案3】:

    valueList 仍然指向单个对象。您可以通过打印出id(valueList) 来检查这一点。 id 给出对象的身份。即对象的内存地址。在您的示例中,如果您打印 id,它将始终相同。

    counter = 0
    appendList = []
    valueList = [[0], [0]]
    
    for i in range(0,3):
        print "id of valueList before", id(valueList)
        #creates a copy of valueList.
        valueList = valueList[:]
        print "id of valueList after", id(valueList)
        valueList[1] = counter
        print "Loop " , i  , " valueList: " , valueList
        print "Appending (valueList): " , valueList , " to (appendList): ", appendList
        appendList.append(valueList)
        counter = counter + 1
    
    
    print "Final appendList: " , appendList
    

    【讨论】:

      【解决方案4】:
      counter = 0
      appendList = []
      valueList = [[0], [0]]
      
      print(valueList[1][0])
      
      for i in range(0,3):
      
          print(counter)
          valueList[1][0] = counter # this was wrong
          print ("Loop " , i  , " valueList: " , valueList)
          print ("Appending (valueList): " , valueList , " to (appendList): " , appendList)
          appendList.append(valueList)
          valueList = [[0], [0]] # and this was absent
          counter += 1
      
      print(appendList)
      
      [[[0], [0]], [[0], [1]], [[0], [2]]]
      

      【讨论】:

        【解决方案5】:

        appendList 包含对valueList 的当前值的引用,因此当您修改valueList 时,appendList 中的副本会更改。每次放入appendList 时,您都需要创建valueList副本

        【讨论】:

          【解决方案6】:

          valueList 每次追加时都是同一个对象,因此在一处修改它似乎会在任何地方修改它。

          >>> a = [0]
          >>> b = a
          >>> a[0] = 42
          >>> b
          [42]
          

          您需要附加它的副本以便每次添加一个新列表。

          appendList.append(valueList[:])
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-09-10
            • 1970-01-01
            • 2012-02-13
            • 2015-08-21
            • 1970-01-01
            • 2011-05-31
            • 2014-05-22
            • 2023-04-04
            相关资源
            最近更新 更多