- SciPy 教程
- SciPy - 首頁
- SciPy - 介紹
- SciPy - 環境設定
- SciPy - 基本功能
- SciPy - 叢集
- SciPy - 常量
- SciPy - FFTpack
- SciPy - 整合
- SciPy - 插值
- SciPy - 輸入和輸出
- SciPy - 線性代數
- SciPy - Nd 影像
- SciPy - 最佳化
- SciPy - 統計
- SciPy - CSGraph
- SciPy - 空間
- SciPy - ODR
- SciPy - 特殊包
- SciPy 的有用資源
- SciPy - 參考
- SciPy - 快速指南
- SciPy - 有用資源
- SciPy - 討論
SciPy - convert_temperature()方法
SciPy convert_temperature()方法用於計算任何刻度形式的溫標,例如攝氏度、開爾文、華氏度和蘭金。
以下是各種刻度的關鍵理解 −
- 攝氏度 − 溫度單位。
- 華氏度 − 這是溫度當水在 32 度時結冰並在 212 度時沸騰的刻度。
- 開爾文 − 這是溫度為 0 時反射的刻度。
- 蘭金 − 絕對溫度單位。
語法
以下是 SciPy convert_temperature() 方法的語法 −
convert_temperature(val, old_scale, new_scale)
引數
此方法接受以下引數 −
- val− 指定溫度範圍值。
- old_scale− 指定原始字串。
- new_scale− 指定將溫度轉換為其中的新字串。
返回值
此函式返回浮點值陣列。
示例 1
以下是基本 SciPy convert_temperature() 方法,說明如何轉換攝氏度、華氏度和開爾文。
import scipy.constants as const
def convert_temperature(value, from_unit, to_unit):
if from_unit == to_unit:
return value
# Convert the input temperature to Celsius first
if from_unit == 'Celsius':
temp_c = value
elif from_unit == 'Fahrenheit':
temp_c = (value - 32) * 5/9
elif from_unit == 'Kelvin':
temp_c = value - const.zero_Celsius
else:
raise ValueError("Unsupported temperature unit")
# Convert from Celsius to the desired unit
if to_unit == 'Celsius':
return temp_c
elif to_unit == 'Fahrenheit':
return temp_c * 9/5 + 32
elif to_unit == 'Kelvin':
return temp_c + const.zero_Celsius
else:
raise ValueError("Unsupported temperature unit")
# Show the result
print(convert_temperature(100, 'Celsius', 'Fahrenheit'))
print(convert_temperature(32, 'Fahrenheit', 'Celsius'))
print(convert_temperature(273, 'Kelvin', 'Celsius'))
輸出
以上程式碼將產生以下結果: −
212.0 0.0 -0.14999999999997726
示例 2
在下面的示例中,使用更多 scipy 常量將基本單位轉換為溫度單位。
import scipy.constants as const
def convert_temperature(value, from_unit, to_unit):
if from_unit == to_unit:
return value
# convert input to Kelvin
if from_unit == 'Celsius':
temp_k = value + const.zero_Celsius
elif from_unit == 'Fahrenheit':
temp_k = (value - 32) * 5/9 + const.zero_Celsius
elif from_unit == 'Kelvin':
temp_k = value
else:
raise ValueError("Unsupported temperature unit")
# convert Kelvin to the desired unit
if to_unit == 'Celsius':
return temp_k - const.zero_Celsius
elif to_unit == 'Fahrenheit':
return (temp_k - const.zero_Celsius) * 9/5 + 32
elif to_unit == 'Kelvin':
return temp_k
else:
raise ValueError("Unsupported temperature unit")
# Show the result
print(convert_temperature(100, 'Celsius', 'Fahrenheit'))
print(convert_temperature(32, 'Fahrenheit', 'Kelvin'))
print(convert_temperature(273.15, 'Kelvin', 'Celsius'))
輸出
以上程式碼將產生以下結果: −
212.0 273.15 0.0
scipy_reference.htm
廣告