Skip to main content
 首页 » 编程设计

python之在函数外打印变异变量时遇到问题 (Python)

2025年05月04日12zlslch

<分区>

def satisfiesF(L): 
""" 
Assumes L is a list of strings 
Assume function f is already defined for you and it maps a string to a Boolean 
Mutates L such that it contains all of the strings, s, originally in L such 
        that f(s) returns True, and no other elements 
Returns the length of L after mutation 
""" 
result = [] 
for l in L: 
    result.extend(l) 
L = list(result) 
for i in result: 
    if i != 'a': 
        L.remove(i) 
return len(L) 

基本上,我要做的是在函数内改变列表 L。从我的测试来看,L 的最终结果似乎正是我想要的结果,但是当我在函数外部打印 L 时,它只是吐出原始的、未改变的列表。谁能帮我解决这个问题?

免责声明:这是一个家庭作业问题,但我并不是在寻找解决整个问题的方法 - 只是这个问题。谢谢!