请问各位为何运行完会是这个结果?
想要使得nums列表中每个数字最多出现两次,请问为何这样运行后,只有-40是出现了三次?nums = [-50,-50,-49,-48,-47,-47,-47,-46,-45,-43,-42,-41,-40,-40,-40,-40,-40,-40,-39,-38,-38,-38,-38,-37,-36,-35,-34,-34,-34,-33,-32,-31,-30,-28,-27,-26,-26,-26,-25,-25,-24,-24,-24,-22,-22,-21,-21,-21,-21,-21,-20,-19,-18,-18,-18,-17,-17,-17,-17,-17,-16,-16,-15,-14,-14,-14,-13,-13,-12,-12,-10,-10,-9,-8,-8,-7,-7,-6,-5,-4,-4,-4,-3,-1,1,2,2,3,4,5,6,6,7,8,8,9,9,10,10,10,11,11,12,12,13,13,13,14,14,14,15,16,17,17,18,20,21,22,22,22,23,23,25,26,28,29,29,29,30,31,31,32,33,34,34,34,36,36,37,37,38,38,38,39,40,40,40,41,42,42,43,43,44,44,45,45,45,46,47,47,47,47,48,49,49,49,50]
for i in nums:
if nums.count(i) > 2:
nums.remove(i)
print(len(nums),"nums:",nums)
运行结果:137 nums: [-50, -50, -49, -48, -47, -47, -46, -45, -43, -42, -41, -40, -40, -40, -39, -38, -38, -37, -36, -35, -34, -34, -33, -32, -31, -30, -28, -27, -26, -26, -25, -25, -24, -24, -22, -22, -21, -21, -20, -19, -18, -18, -17, -17, -16, -16, -15, -14, -14, -13, -13, -12, -12, -10, -10, -9, -8, -8, -7, -7, -6, -5, -4, -4, -3, -1, 1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 16, 17, 17, 18, 20, 21, 22, 22, 23, 23, 25, 26, 28, 29, 29, 30, 31, 31, 32, 33, 34, 34, 36, 36, 37, 37, 38, 38, 39, 40, 40, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 47, 47, 48, 49, 49, 50] nums = [-50,-50,-49,-48,-47,-47,-47,-46,-45,-43,-42,-41,-40,-40,-40,-40,-40,-40,-39,-38,-38,-38,-38,-37,-36,-35,-34,-34,-34,-33,-32,-31,-30,-28,-27,-26,-26,-26,-25,-25,-24,-24,-24,-22,-22,-21,-21,-21,-21,-21,-20,-19,-18,-18,-18,-17,-17,-17,-17,-17,-16,-16,-15,-14,-14,-14,-13,-13,-12,-12,-10,-10,-9,-8,-8,-7,-7,-6,-5,-4,-4,-4,-3,-1,1,2,2,3,4,5,6,6,7,8,8,9,9,10,10,10,11,11,12,12,13,13,13,14,14,14,15,16,17,17,18,20,21,22,22,22,23,23,25,26,28,29,29,29,30,31,31,32,33,34,34,34,36,36,37,37,38,38,38,39,40,40,40,41,42,42,43,43,44,44,45,45,45,46,47,47,47,47,48,49,49,49,50]
for i in nums[:]:
if nums.count(i) > 2:
nums.remove(i)
print(len(nums),"nums:",nums)
逃兵 发表于 2021-4-6 13:46
请问为什么要改成nums[:],我还是不太明白为什么...... 四四四四四秒 发表于 2021-4-6 13:49
请问为什么要改成nums[:],我还是不太明白为什么......
你在遍历列表的时候,把原列表中的元素移除,会导致遍历过程中原列表因为删除了元素而导致的的一个错位行为
所以我们要遍历列表的切片 逃兵 发表于 2021-4-6 13:55
你在遍历列表的时候,把原列表中的元素移除,会导致遍历过程中原列表因为删除了元素而导致的的一个错位行 ...
好的,十分感谢! 四四四四四秒 发表于 2021-4-6 13:49
请问为什么要改成nums[:],我还是不太明白为什么......
比如说 这个列表
遍历会按照顺序从0开始遍历
倘若你在遍历过程中删除第0个元素a,此时列表变为了,他在遍历第1个元素的时候会是c,跳过了元素b
因为他已经遍历过第0个元素,现在要遍历第1个元素,此时元素b变成了第0个元素,c变成了第1个元素,从而导致元素b被跳过了 本帖最后由 四四四四四秒 于 2021-4-6 21:25 编辑
逃兵 发表于 2021-4-6 14:00
比如说 这个列表
遍历会按照顺序从0开始遍历
我明白了,那这个遍历的过程,其实本质是:
第一次循环:i = -48,nums.count(i) < 2,不进入if分支
第二次循环:i = -48,nums.count(i) < 2,不进入if分支
第三次循环:i = -47,nums.count(i) > 2,进入if分支,移除nums中的一个-47,然后由于nums被改变了,所以下一次循环又得从新形成的nums[:]从头开始遍历?也就是从[-48,-48,-47,-47,-46,-46]的第一个元素-48开始。
请问我这样理解对吗?
还是说for i in nums[:]中的nums[:]永远都是不变的?
nums = [-48,-48,-47,-47,-47,-46,-46]
for i in nums[:]:
if nums.count(i) > 2:
nums.remove(i)
print(len(nums),"nums:",nums)
四四四四四秒 发表于 2021-4-6 21:23
我明白了,那这个遍历的过程,其实本质是:
第一次循环:i = -48,nums.count(i) < 2,不进入if分支
...
对的 逃兵 发表于 2021-4-6 23:01
对的
我明白了,感谢解答!
页:
[1]