python数组做参数_python – numpy.array()中的dtype参数

博客探讨了在NumPy中使用`numpy.array()`时,dtype参数如何决定数组元素的数据类型。作者通过示例说明,当传入一个包含不同类型值的Python列表时,NumPy会根据最小的数据类型来创建数组,而不仅仅是考虑值本身。文章指出,Python列表可以保存不同类型的对象,而NumPy数组则要求所有元素具有相同的类型。因此,传入如[10]这样的列表时,NumPy会根据系统默认将其转换为int32,而不是文档中可能暗示的uint8。
摘要由CSDN通过智能技术生成

我试图理解Numpy中dtypes的逻辑.

numpy.min_scalar_type(10)

-> uint8

和:

a = numpy.array([10])

print(a.dtype)

-> int32 (on my machine)

我期待uint8而不是int32,因为(1.9)doc说:

numpy.array(object, dtype=None, …)

dtype : data-type, optional. The desired data-type for the array. If

not given, then the type will be determined as the minimum type

required to hold the objects in the sequence

和:

numpy.min_scalar_type(a)

For scalar a, returns the data type with the smallest size and

smallest scalar kind which can hold its value.

Returns: out : dtype. The minimal data type.

我误解了什么吗?

最佳答案 python列表可以包含不同类型的对象,例如X = [‘apples’,’oranges’,10].如果你输入([10]),你会发现容器的Python类型在技术上被称为列表,而不是数组.

相反,在numpy数组中,所有对象都是相同类型的dtype.

文档告诉您,在创建numpy数组时,dtype设置为将包含所有现有对象的类型.</

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值