list(numpy_array) 和 numpy_array.tolist() 的区别 [英] Difference between list(numpy_array) and numpy_array.tolist()

查看:29
本文介绍了list(numpy_array) 和 numpy_array.tolist() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy 数组上应用 list() 与调用 tolist() 有什么区别?

我正在检查两个输出的类型,它们都表明我得到的结果是一个 list,但是,输出看起来并不完全相同.是不是因为 list() 不是 numpy 特定的方法(即可以应用于任何序列)和 tolist() numpy 特定的,只是在这种情况下它们返回相同的东西?

输入:

points = numpy.random.random((5,2))打印点类型:" + str(type(points))

输出:

点类型:

输入:

points_list = list(points)打印点列表打印Points_list 类型:" + str(type(points_list))

输出:

<预> <代码> [阵列([0.15920058,0.60861985]),阵列([0.77414769,0.15181626]),阵列([0.99826806,0.96183059]),阵列([0.61830768,0.20023207]),阵列([0.28422605,0.94669097])]Points_list 类型:'类型'列表''

输入:

points_list_alt = points.tolist()打印 points_list_alt打印Points_list_alt 类型:" + str(type(points_list_alt))

输出:

<预> <代码> [[0.15920057939342847,0.6086198537462152],[0.7741476852713319,0.15181626186774055],[0.9982680580550761,0.9618305944859845],[0.6183076760274226,0.20023206937408744],[0.28422604852159594,0.9466909685812506]]Points_list_alt 类型:'类型'列表''

解决方案

你的例子已经说明了区别;考虑以下二维数组:

<预><代码>>>>将 numpy 导入为 np>>>a = np.arange(4).reshape(2, 2)>>>一种数组([[0, 1],[2, 3]])>>>a.tolist()[[0, 1], [2, 3]] # 嵌套的香草列表>>>清单(一)[array([0, 1]), array([2, 3])] # 数组列表

tolist 处理到嵌套普通列表的完全转换(即 intlistlist),而 list 只是迭代数组的第一维,创建一个数组列表(np.array of np.int64list of np.array).虽然两者都是列表:

<预><代码>>>>类型(列表(a))<输入列表">>>>类型(a.tolist())<输入列表">

每个列表的元素都有不同的类型:

<预><代码>>>>类型(列表(a)[0])<输入'numpy.ndarray'>>>>类型(a.tolist()[0])<输入列表">

另一个区别,正如您所注意到的,list 可用于任何可迭代对象,而 tolist 只能在专门实现该方法的对象上调用.>

What is the difference between applying list() on a numpy array vs. calling tolist()?

I was checking the types of both outputs and they both show that what I'm getting as a result is a list, however, the outputs don't look exactly the same. Is it because that list() is not a numpy-specific method (i.e. could be applied on any sequence) and tolist() is numpy-specific, and just in this case they are returning the same thing?

Input:

points = numpy.random.random((5,2))
print "Points type: " + str(type(points))

Output:

Points type: <type 'numpy.ndarray'>

Input:

points_list = list(points)
print points_list
print "Points_list type: " + str(type(points_list))

Output:

[array([ 0.15920058,  0.60861985]), array([ 0.77414769,  0.15181626]), array([ 0.99826806,  0.96183059]), array([ 0.61830768,  0.20023207]), array([ 0.28422605,  0.94669097])]
Points_list type: 'type 'list''

Input:

points_list_alt = points.tolist()
print points_list_alt
print "Points_list_alt type: " + str(type(points_list_alt))

Output:

[[0.15920057939342847, 0.6086198537462152], [0.7741476852713319, 0.15181626186774055], [0.9982680580550761, 0.9618305944859845], [0.6183076760274226, 0.20023206937408744], [0.28422604852159594, 0.9466909685812506]]

Points_list_alt type: 'type 'list''

解决方案

Your example already shows the difference; consider the following 2D array:

>>> import numpy as np
>>> a = np.arange(4).reshape(2, 2)
>>> a
array([[0, 1],
       [2, 3]])
>>> a.tolist()
[[0, 1], [2, 3]] # nested vanilla lists
>>> list(a)
[array([0, 1]), array([2, 3])] # list of arrays

tolist handles the full conversion to nested vanilla lists (i.e. list of list of int), whereas list just iterates over the first dimension of the array, creating a list of arrays (list of np.array of np.int64). Although both are lists:

>>> type(list(a))
<type 'list'>
>>> type(a.tolist())
<type 'list'>

the elements of each list have a different type:

>>> type(list(a)[0])
<type 'numpy.ndarray'>
>>> type(a.tolist()[0])
<type 'list'>

The other difference, as you note, is that list will work on any iterable, whereas tolist can only be called on objects that specifically implement that method.

这篇关于list(numpy_array) 和 numpy_array.tolist() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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