【发布时间】:2020-07-15 00:24:55
【问题描述】:
作为初学者,我正在编写一个简单的脚本来更好地熟悉 python。我运行了下面的代码,但没有得到预期的输出。我认为 for 循环在最后一次迭代之前结束,我不知道为什么。
letters = ['a', 'b', 'c', 'c', 'c']
print(letters)
for item in letters:
if item != 'c':
print('not c')
else:
letters.remove(item)
continue
print(letters)
输出返回:
['a', 'b', 'c', 'c', 'c']
not c
not c
['a', 'b', 'c']
预期输出:
['a', 'b', 'c', 'c', 'c']
not c
not c
['a', 'b']
基本上,我不再期望在我的列表中包含“c”。 如果您有更好的方法来编写代码,也将不胜感激。
【问题讨论】:
标签: python python-3.x for-loop if-statement