大佬们,我头都大了,列表前后比较这么费劲吗?
也用了切片了也用了元组了,就是没效果,求大神给支招!lst = [,,,]
lst1 = lst[:]
lst = 1
if lst1 == lst:
print("未改变",1)
else:
print("已改变",1)
tuple2 = tuple(lst[:])
lst = 2
if tuple2 == tuple(lst):
print("未改变",2)
else:
print("已改变",2)
列表嵌套拷贝你会发现
虽然列表不同,但是里面的元素都是同一个ID
lst = [,,,]
lst1 = lst[:]
print(lst1 is lst)
print(lst1 is lst)
这时候可以给每个元素做切片
lst = [,,,]
lst1 = for i in lst]
print(lst is lst1)
print(lst is lst1)
lst = 1
print(lst)
print(lst1)
lst = [,,,]
lst1 = for i in lst]
lst = 1
if lst1 == lst:
print("未改变",1)
else:
print("已改变",1)
逃兵 发表于 2021-2-15 03:46
列表嵌套拷贝你会发现
虽然列表不同,但是里面的元素都是同一个ID
感谢感谢!
页:
[1]