简体   繁体   English

在python中压缩多个if语句

[英]condensing multiple if statements in python

I'm trying to write a function to check if an object is found in multiple lists and to remove the object from any list its found in. I want to know if there is a way to make it cleaner or smarter using some form of generic variable where you predefine the format or something along those lines. 我正在尝试编写一个函数来检查是否在多个列表中找到了一个对象,并从其找到的任何列表中删除该对象。我想知道是否有办法使用某种形式的泛型使其变得更干净或更智能变量,你预定义格式或沿着这些行的东西。 my code in its ugly form: 我的代码以丑陋的形式出现:

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)

What I'm hoping for is something like: 我希望的是:

if choice in list_x:
   list_x.remove(choice)

I would like it to work for each list, would I need to loop through? 我希望它适用于每个列表,我需要循环吗? any suggestions would be great! 任何建议都会很棒! I have the workaround but I would love to learn a more elegant way of coding this! 我有解决方法,但我很想学习更优雅的编码方式!

How about creating a list of lists and looping over that? 如何创建列表并循环遍历?

Something like: 就像是:

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

In a one-liner 在一个班轮

If you use some_list.remove(item) , only the first found match of item is removed from the list. 如果使用some_list.remove(item) ,则只从列表中删除第一个找到的item匹配item Therefore, it depends if the lists possibly include duplicated items, which (all) need to be removed: 因此,这取决于列表是否可能包含重复项,需要删除(全部):

1. If all items in all lists are unique 1.如果所有列表中的所有项目都是唯一的

list1 = ["a", "b" , "c", "d"]
list2 = ["k", "l", "m", "n"]
list3 = ["c", "b", "a", "e"]

[l.remove("a") for l in [list1, list2, list3] if "a" in l]

print(list3)
> ["c", "b", "e"]

However 然而

2. If one or more lists possibly includes duplicated items 2.如果一个或多个列表可能包含重复项目

In that case itertools ' filterfalse() will come in handy: 在这种情况下, itertoolsfilterfalse()会派上用场:

from itertools import filterfalse

def remove_val(val, lists):
    return [list(filterfalse(lambda w: w == val, l)) for l in lists]

l1 = ["a", "b" , "c", "d"]
l2 = ["k", "l", "m", "n"]
l3 = ["a", "c", "b", "a", "a", "e"]

newlists = remove_val("a", [l1, l2, l3])

Then the test: 然后测试:

print(newlists)
> [['b', 'c', 'd'], ['k', 'l', 'm', 'n'], ['c', 'b', 'e']]

Make your list_x a list of all your lists list_x列为所有列表

Then do it this way 然后这样做

for each in list_x:
    if choice in each:
        # if is actually not required
        each.remove(choice)

An alternative to the former suggestions, which is a bit clearer, is to define a helper function. 前一个建议的替代方法是更清楚,它是定义辅助函数。 So, instead of: 所以,而不是:

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)

You'd have: 你有:

def create_newlist(_list, choice):
    if choice in _list:
        _list.remove(choice)

lists = [list_a, list_b, list_c, list_d, list_e]

for _lst in lists:
    create_newlist(_lst, choice)

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

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