在递归调用中添加到列表后,结果将更改

时间:2019-09-21 16:15:09

标签: python list recursion

下面是一个Python程序,用于将8个皇后放在8 * 8网格上,其中没有两个皇后位于相同的行,列和对角线上。

程序看起来正确,但是当我将职位列表附加到列表 results 时,它将更改列表中早期成员的值。

npm ERR! Can't install /Users/[username]/Documents/firebaseNode/functions: Missing package version

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/[username]/.npm/_logs/2019-09-21T16_11_22_371Z-debug.log```



I have tried to locate the missing packages but cannot get any information. 

```17 verbose stack Error: Can't install /Users/[username]/Documents/nodeFolder/functions: Missing package version
17 verbose stack     at hasMinimumFields (/usr/local/lib/node_modules/npm/lib/install/validate-args.js:28:15)
17 verbose stack     at Array.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/slide/lib/bind-actor.js:15:8)
17 verbose stack     at LOOP (/usr/local/lib/node_modules/npm/node_modules/slide/lib/chain.js:15:14)
17 verbose stack     at chain (/usr/local/lib/node_modules/npm/node_modules/slide/lib/chain.js:20:5)
17 verbose stack     at /usr/local/lib/node_modules/npm/lib/install/validate-args.js:16:5
17 verbose stack     at /usr/local/lib/node_modules/npm/node_modules/slide/lib/async-map.js:52:35
17 verbose stack     at Array.forEach (<anonymous>)
17 verbose stack     at /usr/local/lib/node_modules/npm/node_modules/slide/lib/async-map.js:52:11
17 verbose stack     at Array.forEach (<anonymous>)
17 verbose stack     at asyncMap (/usr/local/lib/node_modules/npm/node_modules/slide/lib/async-map.js:51:8)
17 verbose stack     at module.exports (/usr/local/lib/node_modules/npm/lib/install/validate-args.js:15:3)
17 verbose stack     at Array.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/slide/lib/bind-actor.js:15:8)
17 verbose stack     at LOOP (/usr/local/lib/node_modules/npm/node_modules/slide/lib/chain.js:15:14)
17 verbose stack     at chain (/usr/local/lib/node_modules/npm/node_modules/slide/lib/chain.js:20:5)
17 verbose stack     at Installer.loadAllDepsIntoIdealTree (/usr/local/lib/node_modules/npm/lib/install.js:477:3)
17 verbose stack     at Array.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/slide/lib/bind-actor.js:15:8)
18 verbose cwd /Users/[username]/Documents/nodeFolder/functions
19 verbose Darwin 17.7.0
20 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g"
21 verbose node v8.11.4
22 verbose npm  v6.11.3```

1 个答案:

答案 0 :(得分:1)

问题是您要重复将columns的相同实例添加到结果列表。这是正在发生的事情的简化示例

>>> results = []
>>> columns = ["original column value"]
>>> results.append(columns)
>>> results.append(columns)
>>> results
[['original column value'], ['original column value']]
>>> columns[0] = "A new value"
>>> results
[['A new value'], ['A new value']]
>>> 

要解决此问题,您需要复制列列表,以免修改已经添加到结果中的列

复制列表的一种简单方法是通过columns = columns[:]对其进行切片

columns函数中复制placeQueen列表应该可以解决问题

def placeQueen(row, columns):
    # creates a copy of the columns list
    columns = columns[:]
    if (row == GRID_SIZE):
        print(columns)  # it is where I print the positions
        return [columns]  # it is where I append the positions


    results = []
    for col in range(GRID_SIZE):
        if (checkVlaid(row, col, columns)):
            columns[row] = col

            results += placeQueen(row + 1, columns)

    return results