专题文章
时长:00:00更新时间:2020-11-27 14:22:04
我们直接先给出输出与预期不同的代码。In[28]: a = [1,2,3,4,5,6]In[29]: for i in a: ...: a.remove(i) ...: In[30]: aOut[30]: [2.4.6]。在上述for循环中,假设我们删除了index=2的值,原本index=3及之后的值会向前补位,所以在循环中就跳过了原index=3的变量。同理,使用list.pop()函数删除指定元素的时候,也会出现上述情况,如。In[33]: a = [1,2,3,4,5,6]In[34]: for index.value in enumerate(a): ...: a.pop(index) ...: In[35]: aOut[35]: [2.4.6]。
查看详情