在Python中评估布尔元组 [英] Evaluate boolean tuple in Python

查看:60
本文介绍了在Python中评估布尔元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让它评估为假.

I'm trying to get this to evaluate to false.

(False,)

当前等于true,因为我认为元组不是空的.那么如何将其提取或转换为布尔值呢?谢谢〜

It's currently equaled to true, because I think the tuple is not empty. So how might one extract or cast this to a boolean? Thanks~

推荐答案

从元组中提取元素是最简单的方法:

Extract the element from the tuple is the most simple approach:

value = (False,)[0]

Python2更为宽大,但通常来说,出于比较目的将元组视为单个值不是一个好习惯(Python3明确禁止使用它)

Python2 is more lenient, but in general it isn't good practice to treat a tuple as a single value for comparison purposes (Python3 explicetly bans it)

相反,请查看

all

any

用于此行为的功能.与往常一样,文档是您的朋友:

functions for this behavior. As always, the documentation is your friend:

https://docs.python.org/2/library/functions.html#all

这篇关于在Python中评估布尔元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆