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''
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…