我如何从一品脱数量列表中创建一个numpy数组 [英] how do I make a numpy array from a list of pint quantities

查看:113
本文介绍了我如何从一品脱数量列表中创建一个numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import numpy as np
from pint import UnitRegistry 
unit = UnitRegistry()
Q_ = unit.Quantity
a = 1.0*unit.meter
b = 2.0*unit.meter
# some calculations that change a and b 
x=np.array([a.magnitude,b.magnitude])*Q_(1.0,a.units)

将从变量a和b生成一个numpy数组,它们是品脱数量.由于不能保证a和b具有相同的单位,因此有些粗糙.有没有更清洁的方法呢?我需要编写一个函数吗?

will make a numpy array from variables a and b which are Pint quantities. It is somewhat crude as there is no guarantee that a and b have the same units. Is there a cleaner way to do it? Do I need to write a function?

推荐答案

另一个选项,它允许元素具有不同单位的数组,可能是以效率和乐趣为代价的,是给数组 dtype ='对象" .

Another option, which allows for arrays whose elements have different units, probably at the cost of some efficiency and fun is to give the array dtype='object'.

import numpy as np
from pint import UnitRegistry 
unit = UnitRegistry()
a = 1.0*unit.meter
b = 2.0*unit.meter
# some calculations that change a and b 
x=np.array([a, b], dtype='object')

这篇关于我如何从一品脱数量列表中创建一个numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆