0

我对 python 和 python pint 很陌生。我只想知道python pint的可支持单位。以下表示的含义是什么:

{length: 1, time: -1}

来自 品脱单位的参考。另外,我如何打印能量和温度的可用品脱单位。

from pint import UnitRegistry
ureg = pint.UnitRegistry()
ureg.Unit
4

1 回答 1

1

字典的键是维度,对应的值是维度的幂。也就是说,{length: 1, time: -1}是速度的表示,一个由字典表示维数的单位的例子是 m/s。对于加速,表示将是{length: 1, time: -2}

In [13]: ureg.Unit('m/s').dimensionality
Out[13]: <UnitsContainer({'[length]': 1, '[time]': -1})>

In [14]: ureg.Unit('m/s^2').dimensionality
Out[14]: <UnitsContainer({'[length]': 1, '[time]': -2})>

要获得给定维度的可用单位,请使用UnitRegistry.get_compatible_units; 例如,对于您的两个示例:

In [9]: ureg.get_compatible_units('[energy]')
Out[9]:
frozenset({<Unit('electron_volt')>,
           <Unit('rydberg')>,
           <Unit('hartree')>,
           <Unit('erg')>,
           <Unit('joule')>,
           <Unit('foot_pound')>,
           <Unit('calorie')>,
           <Unit('fifteen_degree_calorie')>,
           <Unit('international_calorie')>,
           <Unit('atmosphere_liter')>,
           <Unit('thermochemical_british_thermal_unit')>,
           <Unit('international_british_thermal_unit')>,
           <Unit('british_thermal_unit')>,
           <Unit('watt_hour')>,
           <Unit('US_therm')>,
           <Unit('therm')>,
           <Unit('ton_TNT')>,
           <Unit('tonne_of_oil_equivalent')>,
           <Unit('quadrillion_Btu')>})

In [10]: ureg.get_compatible_units('[temperature]')
Out[10]:
frozenset({<Unit('degree_Rankine')>,
           <Unit('kelvin')>,
           <Unit('degree_Fahrenheit')>,
           <Unit('degree_Reaumur')>,
           <Unit('degree_Celsius')>,
           <Unit('atomic_unit_of_temperature')>,
           <Unit('planck_temperature')>})
于 2021-01-30T12:19:26.460 回答