首页 > 解决方案 > 如何在元组中使用元组的长度?

问题描述

我正在尝试查看元组中的元组是否只有数字 2,任意次数

def check_tupl(tpl):
    for i in range(len(tpl)):
        tuple(tpl[i])                   
        for i in range(len(tpl[i][i])):
            if tpl[i][i]==2:
                return True
            else:
                return False

它向我显示了此错误消息

builtins.TypeError:“int”类型的对象没有 len()

标签: pythontuples

解决方案


我们可以使用anyandall来有效地检查这一点。

def check_tupl(tpls):
    return any(all(v == 2 for v in tpl) for tpl in tpls)

推荐阅读