因為a = b
,而list為可變物件,所以a就是b。
你要用copy方法拿到一個跟a一樣的b。
https://ithelp.ithome.com.tw/articles/10221255
>>> a = [1, 2]
>>> b = a
>>> c = a.copy()
>>> id(a)
1927327460992
>>> id(b)
1927327460992
>>> id(c)
1927330636672
>>> a
[1, 2]
>>> b
[1, 2]
>>> c
[1, 2]
>>> c.reverse()
>>> a
[1, 2]
>>> b
[1, 2]
>>> c
[2, 1]
>>> a.reverse()
>>> a
[2, 1]
>>> b
[2, 1]
>>> c
[2, 1]