在 Python 中将数组作为参数传递

Passing an array as an argument in Python

我不确定这是否可行,但我正在尝试创建一个 python 程序来识别多项式并识别它们的所有属性。我试图创建一个类似于 switch() 函数的函数,并且我打算为每个参数案例创建数百个函数,我想将其中一个参数设为数组,目前它是给我一堆错误,我真的不知道我应该做什么,因为他们没有解释自己,我环顾四周,没有发现任何有用的东西,任何帮助将不胜感激,我相当确定 python 中有类似的功能,但是关于它的任何文章都非常混乱,谢谢,下面是我试图制作的功能。

def switch(checked, check):
    for(item in check):
        if(item == check):
            return True
    
    return False

你是这个意思吗?

def switch(checked, check):
    for item in check:
        if item == checked:
            return True
    return False

要检查某物是否是列表中的一项,您不需要遍历列表。您可以只使用 in 运算符:

d = ['abc', 'xyz', 1, 99]

if 'abc' in d:
    # True
    # do something

if 'mno' in d:
    # False
    # do something

如果您需要模拟 switch 语句,您可以使用像这样的辅助函数:

def switch(v): yield lambda *c: v in c

然后您可以以类 C 风格使用它:

x = 3
for case in switch(x):
    if case(1,2):
       # do something
       break
    if case(3):
       # do something else
       break
    if case(4,5,7):
       # do some other thing
       break
else:
    # handle other cases

或者您可以使用 if/elif/else 语句:

x = 3
for case in switch(x):
    if   case(1,2):   # do something
    elif case(3):     # do something else
    elif case(4,5,7): # do some other thing
    else:             # handle other cases