Python总体评估

2024-10-01 04:46:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个列表列表(listA)和另一个单独的列表(listB)。我试图检查listB是否与listA中的任何列表匹配,这是基于我在testing12函数中所做的类型和位置。我的问题是我该怎么做才能让if语句给我一个全面的评价,即正确或错误。如果有一个或多个匹配项,则为True;如果根本没有匹配项,则为False

listA = [[3,"alpha"], [7, 8], ["hat", "bat"]]

listB = [5,5]


def testing12(x,y):
    result = map(type, x) == map(type, y)
    return result


for n in lists:
    if testing12(list,n) == True:
       print "True"
    else:
       print "False"    

Tags: 函数falsetrue类型map列表iftype
1条回答
网友
1楼 · 发布于 2024-10-01 04:46:16

要使用^{}^{}内置程序:

if any(testing12(list, n) for n in lists):
    print("at least one matched")

或:

if all(testing12(list, n) for n in lists):
    print("All lists matched")

相关问题 更多 >