python - 如何在元组中使用元组的长度?
问题描述
我正在尝试查看元组中的元组是否只有数字 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()
解决方案
我们可以使用any
andall
来有效地检查这一点。
def check_tupl(tpls):
return any(all(v == 2 for v in tpl) for tpl in tpls)
推荐阅读
- docker - 安装 docker 时证书验证失败
- ios - 如何立即将数据传送到 iPhone 应用程序?
- r - 在闪亮的应用程序中缩放 dgr_graph 的更好方法
- java - 如何在我的 Java 构造函数中设置模板?
- django - 生成固定装置时的django反序列化错误
- python - 列表中的格式化字符串
- iis - Squid:使用 IIS 10 的反向代理
- javascript - VueJS 在构建之外引用本地 JSON 文件
- python - 如何根据另一列统计DataFrame中某个值出现的次数?
- arduino - 如何通过 5*5 按钮控制 5*5 Led 中的 Led - 矩阵