Python には、温度単位を変換するための複数の方法が用意されています。NumPy ライブラリの numpy.convert_units() 関数を使用します。NumPy をインポートし、変換に Convert_units を使用します。 Pandas ライブラリの pandas.to_numeric() 関数を使用します。Pandas をインポートし、to_numeric を使用して温度をインテリジェントに変換します。カスタム関数を使用する: 変換用のカスタム関数 fahrenheit_to_celsius および celsius_to_fahrenheit を作成します。

#Python での温度変換
#Python には、温度単位を変換するためのさまざまな方法が用意されています。NumPy の使用
NumPy ライブラリのnumpy.convert_units() 関数を使用して、温度単位を変換できます:
<code class="python">import numpy as np # 从华氏度转换为摄氏度 celsius = np.convert_units(100, 'degF', 'degC') # 从摄氏度转换为华氏度 fahrenheit = np.convert_units(37, 'degC', 'degF')</code>
Pandas の使用
Pandas ライブラリのpandas.to_numeric() 関数は、温度単位をインテリジェントに変換できます:
<code class="python">import pandas as pd
# 从华氏度转换为摄氏度
celsius = pd.to_numeric("100 degF", unit='degF', errors='coerce')
# 从摄氏度转换为华氏度
fahrenheit = pd.to_numeric("37 degC", unit='degC', errors='coerce')</code>カスタム関数を使用する
温度単位を変換するカスタム関数を作成することもできます:<code class="python">def fahrenheit_to_celsius(fahrenheit):
"""将华氏度转换为摄氏度"""
return (fahrenheit - 32) * 5/9
def celsius_to_fahrenheit(celsius):
"""将摄氏度转换为华氏度"""
return celsius * 9/5 + 32</code>Example
これらのメソッドを使用して温度単位を変換する方法は次のとおりです:rreee
以上がPython123で温度変換を書く方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。