|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位大神,想问结果为什么能输出[1,2,3,4,5,0]呢?
list1.clear()后不应该是输出[]吗?后面的三条语句如何理解呢?谢谢。
list1 = [1,2,3,4,5,5,3,1,0]
temp = list1[ : ]
list1.clear()
for each in temp:
if each not in list1:
list1.append(each)
结果输出:
list1
[1, 2, 3, 4, 5, 0]
本帖最后由 小伤口 于 2020-12-9 22:30 编辑
- list1 = [1,2,3,4,5,5,3,1,0]
- temp = list1[ : ]#list1拷贝到temp(list1被删除,拷贝的不会,拷贝相当于克隆,另一个不会受到影响)
- list1.clear()#list1 变成空列表
- for each in temp:#for循环temp里的内容
- if each not in list1:#如果循环temp的内容list1里面没有
- list1.append(each)#就将for循环temp的内容添加到list1里面
- #因为temp是拷贝的简而言之就是通过for循环再一次把list1拷贝的加入到被清空的lisr1里面
复制代码
|
|