繁体   English   中英

Python中for .. in循环的意外输出

[英]Unexpected output of for.. in loop in Python

我想运行一个for循环来检查字符串中的每个字母,如果wanted列表包含该字母,那么我想要保留该字母,如果wanted不包含该字母,那么我想要将其删除。

phrase = "Don't panic"
pList = list(phrase)
print(pList)
wanted = ["o","n","t","a","p"]
for anything in pList:
    print("Now checking", anything)
    if anything not in wanted:
        print("Removed",anything) 
        pList.remove(anything)
    elif anything in wanted:
        print("Didn't remove", anything)

print(pList)

以下代码返回此输出-

['D', 'o', 'n', "'", 't', ' ', 'p', 'a', 'n', 'i', 'c']                                                   

Now checking D                                                                                       
Removed D                                                                                                                                                                                           
Now checking n                                                                                          
Didn't remove n                                                                                         
Now checking '                                                                                           
Removed '                                                                                                                                                                                               
Now checking                                                                                              
Removed                                                                                                                                                                                                 
Now checking a                                                                                           
Didn't remove a                                                                                         
Now checking n                                                                                           
Didn't remove n                                                                                           
Now checking i                                                                                          
Removed i                                                                                                                                                                                       
['o', 'n', 't', 'p', 'a', 'n', 'c']   

该代码不会删除最后一个字母(即“ c”),并且字母“ o”,“ t”,“ p”和“ c”没有显示“ Now checking输出。

我尝试删除一些行,然后才运行-

phrase = "Don't panic"
pList = list(phrase)
print(pList)
wanted = ["o","n","t","a","p"]
for anything in pList:
    print("Now checking", anything)

这次输出有意义-

Now checking D                                                                                            
Now checking o                                                                                            
Now checking n                                                                                            
Now checking '                                                                                            
Now checking t                                                                                            
Now checking                                                                                              
Now checking p                                                                                            
Now checking a                                                                                            
Now checking n                                                                                            
Now checking i                                                                                            
Now checking c 

那么为什么在运行完整代码时不发生这种情况?

遍历列表时,您正在编辑列表。 因此,大小将在中途更改,最终您将获得怪异的行为。 相反,您可以创建一个新列表来保存已过滤的字符。

phrase = "Don't panic"
pList = list(phrase)
new_plist = []
wanted = ["o","n","t","a","p"]
for i, anything in enumerate(pList):
    print("Now checking", anything)
    if anything in wanted:
        print("Didn't remove", anything)
        new_plist.append(anything)
    else:
        print("Removed", anything)

print(new_plist)

使用列表推导的一种更简单的方法是

pList = ''.join([i for i in pList if i not in wanted])

其他答案和评论都是100%正确的-切勿在迭代列表时修改列表! 可能会解决此类问题的一些事情是反过来思考问题:不要从列表中删除 ,而是反转条件并添加到新列表中。 这样,它会基于输入建立新的输出,而不是突变输入=减少副作用,更简洁的代码,函数式编程原理等。@coldspeed的列表理解方法是一种不错的解决方案。

对原始代码的更改最少的替代解决方案:

phrase = "Don't panic"
pList = list(phrase)
print(pList)
wanted = ["o","n","t","a","p"]
finalList = []
for anything in pList:
    print("Now checking", anything)
    if anything in wanted:
        finalList.append(anything)
        print("Didn't remove", anything)
    else:
        print("Removed",anything)

print(finalList)

现在pList不会被修改,新的finalList包含所需的结果。 (编辑:意识到@coldspeed的代码现在几乎与此完全相同-当我最初提交时不是那样。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM