作者: Tszlone 時間: 2019-10-21 19:40 標題: Python list問題
- l1 = [5, 6]
- l2 = [7, 8, l1]
- l1 = [2, 3]
- print(l2)
上面段code點解係輸出[7, 8, [5, 6]],而吾係[7, 8, [2, 3]]?
下面段code就得
- l1 = [5, 6]
- l2 = [7, 8, l1]
- l1[0] = 2
- l1[1] = 3
- print(l2)
Thanks
PS: Python新手
作者: CVSDF 時間: 2019-10-21 20:11
I guess at line 03, l1 = [2, 3], l1 is pointing to a new List already (very bad variable name btw, hard to read), but it doesn't change l2[2] (which still points to the old List[5,6]. making l2[2] the only way to access [5,6].
作者: harry6677 時間: 2019-10-22 17:12
I guess at line 03, l1 = [2, 3], l1 is pointing to a new List already (very bad variable name btw, h ...
CVSDF 發表於 2019-10-21 20:11
正解,
另外樓主可以上呢個網試吓你段code,睇吓每一行code執行的情況。
http://www.pythontutor.com/visualize.html#mode=edit
作者: Tszlone 時間: 2019-10-22 22:36
thanks
作者: 望月小妖 時間: 2019-10-23 00:32
老餅唔識Python,應該係
pass by reference
pass by value
嘅分別

