当我完全不更新jaunt时,jaunt的值(特别是第一个值)为什么会改变?我只是更新'o'

问题描述 投票:0回答:1
start = [2020,0,0,2020]
jaunts = [[2020,0,0,2021],[2021,0,0,2022],[2022,0,0,2023],[2020,1,1,2023],[2021,0,0,2023]]

def gridneighbors(start,jaunts):
    neigh = []
    for o in jaunts:
        new_cell = o
        if start[0]==o[0] and (start[1] == o[1] and start[2] == o[2]):
            new_cell[0]=o[3]
            neigh.append(o)
        elif start[3]==o[3] and (start[1] == o[1] and start[2] == o[2]):
            o[3]=o[0]
            neigh.append(o)
        print(jaunts)
    return neigh

print(gridneighbors(start,jaunts))

output:
[[2021, 0, 0, 2021], [2021, 0, 0, 2022], [2022, 0, 0, 2023], [2020, 1, 1, 2023], [2021, 0, 
0, 2023]]

这是我得到的jaunts的值,即使我没有更新它,第一个值也已更改。

python-3.x list for-loop if-statement assignment-operator
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.