Python第二版第5章(第45页)的代码为什么执行出来列表结果不对
课本中的代码:old_list = ["西班牙", "葡萄牙", "葡萄牙", "牙买加", "匈牙利"]
new_list = []
for x in old_list:
if x not in old_list:
new_list.append(x)
print(new_list)
为什么执行下来的结果new_list的结果不是["西班牙","葡萄牙", "牙买加", "匈牙利"]而是[]空列表呢? if x not in old_list:
???x来自于old_list,你又判断他不在old_list里的时候,new_list采取添加... old_list = ["西班牙", "葡萄牙", "葡萄牙", "牙买加", "匈牙利"]
new_list = []
for x in old_list:
if x not in new_list:
new_list.append(x)
print(new_list)
你想要的代码是这样的 SHRS23 发表于 2020-7-1 15:13
old_list = ["西班牙", "葡萄牙", "葡萄牙", "牙买加", "匈牙利"]
new_list = []
for x in old_list:
明白了,谢谢大神解答。{:5_110:} yhhpf 发表于 2020-7-1 15:09
if x not in old_list:
???x来自于old_list,你又判断他不在old_list里的时候,new_list采取添加...
抄书抄错了,楼下已解释,感谢回答。{:5_110:}
页:
[1]