【问题标题】:Python Overall evaluationPython 整体评测
【发布时间】:2016-02-28 21:48:59
【问题描述】:

我有一个列表列表 (listA) 和另一个单独的列表 (listB)。我正在尝试根据我在 testing12 函数中完成的类型和位置检查 listB 是否与 listA 中的任何列表匹配。我的问题是我该怎么做才能使 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"    

【问题讨论】:

  • testing12(list, n) 中的list 是什么?如果是内置的listmap(type, list)会报错。
  • sorry list in testin12(list, n) 是 listB

标签: python python-2.7


【解决方案1】:

您想使用allany 内置函数:

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")

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2010-11-29
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 2014-10-25
    • 2012-07-25
    • 2016-04-12
    相关资源
    最近更新 更多