Numpy中 tolist() 的用法

ndarray.tolist():

  • 将(多维)数组作为(嵌套)列表返回
  • 将数组数据的副本作为(嵌套)Python列表返回
  • 数据项将转换为最接近的兼容内置Python类型
  • 如果ndarray.ndim是0,则返回Python标量

原英文释义如下:

  • Return the array as an ndarray.ndim-levels deep nested list of
    Python scalars
  • Return a copy of the array data as a (nested)
    Python list.
  • Data items are converted to the nearest compatible
    builtin Python type, via the ~numpy.ndarray.item function.
  • If ndarray.ndim is 0, then since the depth of the nested list is 0,
    it will not be a list at all, but a simple Python scalar.
import numpy as np

A = array([1, 2, 3])
print(type(A))        # 结果 <class 'numpy.ndarray'>
B = A.tolist()        # 结果 [1, 2, 3]
print(B)
print(type(B))        # 结果 <class 'list'>
C = mat(A)
print(C)              # 结果 [[1 2 3]]
print(type(C))        # 结果 <class 'numpy.matrix'>
D = C.tolist()
print(D)              # 结果 [[1, 2, 3]]
print(type(D))        # 结果 <class 'list'>
E = C.tolist()[0]
print(E)              # 结果 [1, 2, 3]
print(type(E))        # 结果 <class 'list'>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值