Python 中将布尔值转换为整数的方法


Python 是一种广泛使用的编程语言,用于世界各地各种目的,例如 Web 开发、数据科学、机器学习以及执行各种自动化流程。布尔值的输出形式为 True 和 False。因此,如果我们想将其转换为整数,我们可以将 True 表示为 1,将 False 表示为 0。在本文中,我们将学习将布尔值转换为整数的不同方法。

将布尔值转换为整数的不同方法

整数函数

在这种方法中,我们将运行整数函数以及布尔值作为参数,因此,我们将自动以整数的形式接收输出。让我们看一个例子,以便更好地理解它:-

示例

# Case 1
boolean_output = True # The output of boolean is provided as input
integer_output = int(boolean_output) # With the help of integer function the boolean value will be converted into integer
print(integer_output)

# Case 2
boolean_output = False # The output of boolean is provided as inputinteger_output = int(boolean_output) # With the help of integer function the boolean value will be converted into integer
print(integer_output) 

输出

上面示例的输出如下:-

1
1

乘法运算

在这种方法中,我们只需将相应的布尔输出乘以 1 和 0,具体取决于输出。由于布尔值具有执行数学运算的能力,因此此操作可以轻松执行。让我们看一个例子,以便更好地理解它:-

示例

# Case 1
boolean_output = True # The output of boolean is provided as input
integer_output = boolean_output * 1 # Multiplication operation of the boolean output is carried out 
print(integer_output) 

# Case 2
boolean_output = False # The output of boolean is provided as inputinteger_output = boolean_output * 1 # Multiplication operation of the boolean output is carried out 
print(integer_output) 

输出

上面示例的输出如下:-

1
1

带条件表达式的整数函数

条件值用于根据布尔输出返回值。它在输出为 True 时显示一个不同的值,在输出为 False 时显示另一个值。让我们看一个例子,以便更好地理解它:-

示例

# Case 1
boolean_output = True # The output of boolean is provided as input
integer_output = int(boolean_output) if boolean_output else 0 # A condition is provided to show 1 as output if boolean value is true or else the output will be shown as 0
print(integer_output) 

# Case 2
boolean_output = False # The output of boolean is provided as input
integer_output = int(boolean_output) if boolean_output else 0 # A condition is provided to show 1 as output if boolean value is true or else the output will be shown as 0
print(integer_output) 

输出

上面示例的输出如下:-

# Case-1
1 # True value is represented as 1
# Case-2
0 # False Value is represented as 0

带布尔算术的整数函数

在这种方法中,将在布尔值上执行算术运算。让我们看一个例子,以便更好地理解它:-

示例

# Case 1
boolean_output = True # The output of boolean is provided as input
integer_output = int(boolean_output + 0) # Arithmetic operation is performed on the boolean output to convert it into integer. If the output is true, it will take its value as 1 and perform the operation and if its output is false, it will take its value as 0 and perform the operation
print(integer_output) 

# Case 2
boolean_output = False # The output of boolean is provided as input
integer_output = int(boolean_output + 0) # Arithmetic operation is performed on the boolean output to convert it into integer. If the output is true, it will take its value as 1 and perform the operation and if its output is false, it will take its value as 0 and perform the operation
print(integer_output) 

输出

上面示例的输出如下:-

1
0

| 运算符

在这种方法中,我们将使用 | 运算符并根据布尔输出分配值。让我们看一个例子,以便更好地理解它:-

示例

# Case 1
boolean_output = True # The output of boolean is provided as input
integer_output = boolean_output | 0# The bitwise operator is provided with the value of 0, which will be reversed and displayed in the output in the case of true as the output
print(integer_output) 

# Case 2
boolean_output = False # The output of boolean is provided as input
integer_output = boolean_output | 0 # The bitwise operator is provided with the value of 0, which will be displayed as it is when the boolean value is false
print(integer_output) 

输出

上面示例的输出如下:-

1
0

字典映射

在这种方法中,我们将创建一个字典,将值分配给布尔值。让我们看一个例子,以便更好地理解它:-

示例

# Case 1
boolean_output = True # The output of boolean is provided as input
boolean_to_integer = {True: 1, False: 0} # We create a dictionary to map the boolean values to the correct integer values
integer_output = boolean_to_integer[boolean_output] # This dictionary is run along with the boolean value function and the respective value is displayed as the output
print(integer_output) 

# Case 2
boolean_output = False # The output of boolean is provided as input
boolean_to_integer = {True: 1, False: 0} # We create a dictionary to map the boolean values to the correct integer valuesinteger_output = boolean_to_integer[boolean_output] # This dictionary is run along with the boolean value function and the respective value is displayed as the output
print(integer_output) 

输出

上面示例的输出如下:-

1
1

结论

可以参考上文,了解可以使用哪些不同的方法将布尔值转换为 Python 中的整数,并根据方便使用上述任何一种方法。

更新于: 2023年8月7日

1K+ 次查看

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告