1. 程式人生 > >python學習====復制list

python學習====復制list

nbsp col pan bsp 改變 clas cnblogs 數據復制 學習

"""將一個列表的數據復制到另一個列表中。"""

"""
使用[:]

"""

a = [1,2,3]
b = a[:]
print b       # 將a的數據賦值給b 當a的數值發生改變時b不變
[1, 2, 3]
a = [1,2,3]
b = a
print b       # 將a的地址賦值給b 當a的數值發生改變時b隨之改變
[1, 2, 3]
a = [1,2,3]
b = a[:]
print b
a[0] = 0
print a        # 將a的數據賦值給b 當a的數值發生改變時b不變
print b
[1, 2, 3]
[0, 2, 3]
[1, 2, 3]
a 
= [1,2,3] b = a print b a[0] = 0 print a # 將a的地址賦值給b 當a的數值發生改變時b隨之改變 print b

python學習====復制list