在python中压缩多个if语句

Bri*_*n F 6 python loops if-statement list

我试图写一个函数来检查,如果一个对象在多个列表中发现,并从它的.我想知道是否有一种方法更干净或更聪明的使用某种形式的通用,使发现的列表中删除对象变量,你预定义格式或沿着这些行的东西.我的代码以丑陋的形式出现:

def create_newlist(choice):

    if choice in list_a:
        list_a.remove(choice)
    if choice in list_b:
        list_b.remove(choice)
    if choice in list_c:
        list_c.remove(choice)
    if choice in list_d:
        list_d.remove(choice)
    if choice in list_e:
        list_e.remove(choice)
Run Code Online (Sandbox Code Playgroud)

我希望的是:

if choice in list_x:
   list_x.remove(choice)
Run Code Online (Sandbox Code Playgroud)

我希望它适用于每个列表,我需要循环吗?任何建议都会很棒!我有解决方法,但我很想学习更优雅的编码方式!

gow*_*ath 6

如何创建列表并循环遍历?

就像是:

lists = [list_a, list_b, list_c, list_d, list_e]
for lst in lists: 
    if choice in lst: 
        lst.remove(choice)
Run Code Online (Sandbox Code Playgroud)


be_*_*ood 2

列出list_x所有清单

然后这样做

for each in list_x:
    if choice in each:
        # if is actually not required
        each.remove(choice)
Run Code Online (Sandbox Code Playgroud)