首页 > 代码库 > python学习====复制list

python学习====复制list

"""将一个列表的数据复制到另一个列表中。"""

"""
使用[:]

"""

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