import numpy as np
>>> np.arange(24).reshape(2,3,4)
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> t1 = np.arange(24).reshape(2,3,4)
>>> t1
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> t1.reshape(4,6)
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
>>>
1.直接打印了t1,说明reshape函数是有返回值的,是有return语句的,有rerurn语句的函数往往不会对t1本身的值发生改变的,t1还是原来的t1,只是赋值给了新的变量,也就是有新的变量接收了返回值,然后直接打印。但列表里面调用extend方法时,列表本身发生了变化,将另一个列表放到了里面来了,也就是原地操作,原地操作无返回值。如果无法判断函数是否有返回值,看输出,是none则为原地操作,有输出则有返回值
t1 = np.arange(24).reshape(2,3,4)
>>> t1
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> t1.reshape
<built-in method reshape of numpy.ndarray object at 0x0000025222A89F80>
>>> t1.reshape(4,6)
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
>>> t1
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> t1 = t1.reshape(4,6)
>>> t1
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
t1无变化,需再赋值才会变化。变成一维的操作。
t1.reshape(24,)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
t1.flatten()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
原创不易,随便转载!请指出错误和见解