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+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.