创建函数以从嵌套列表中删除某些元素

:     '''     去完成。     '''

When I run ```eliminate_elements([['N','L'],['G','N',['N','B']],'L'])```,I want the output to be ```[[],[,['B']]``` Why is 'L' not removed when it clearly is an element in to_eliminate?
Please note that I do not want built in python functions to do this,I have to create my own function that does not rely on any outside modules or functions.
mz202 回答:创建函数以从嵌套列表中删除某些元素

# your code goes here

def eliminate_elements(my_list,to_eliminate):
    '''
    To complete.
    '''

    for sub_list in my_list:
        for i in to_eliminate:
            if i in sub_list:
                occu = sub_list.count(i)
                for _ in range(occu):
                    sub_list.remove(i)

    return my_list  

print(eliminate_elements( [['N','L'],['G','N',['N','B']],'L']))

输出

[[],['G'],['B']]
本文链接:https://www.f2er.com/3150149.html

大家都在问