python: zip 与 * 探究

来源:互联网 发布:js获取传入的参数 编辑:程序博客网 时间:2024/05/29 16:21

结论

  因为之前对python中的 zip * ,搞得不是很清楚,这次做项目时又遇到这个问题,所以上网查阅,并经过自己编写代码进行实验,得出以下结论:

作用域 zip() zip(* ) 简述 将list合并打包 将list进行unpack 在rank上的变化 插入axis=1,其余不变 将axis=0和axis=1对换,其余不变 在type上的变化 ndarray –> list ndarray –> list


附上自己写的实验源码

import numpy as npa = np.zeros(shape=[3, 5, 6], dtype=np.int)print np.shape(a), ':', type(a)for b in a:    print np.shape(b)printprint np.shape(zip(a)), ':', type(zip(a))for b in zip(a):    print np.shape(b)printprint np.shape(zip(zip(a))), ':', type(zip(zip(a)))for b in zip(zip(a)):    print np.shape(b)printprint np.shape(zip(*a)), ':', type(zip(*a))for b in zip(*a):    print np.shape(b)
(3, 5, 6) : <type 'numpy.ndarray'>(5, 6)(5, 6)(5, 6)(3, 1, 5, 6) : <type 'list'>(1, 5, 6)(1, 5, 6)(1, 5, 6)(3, 1, 1, 5, 6) : <type 'list'>(1, 1, 5, 6)(1, 1, 5, 6)(1, 1, 5, 6)(5, 3, 6) : <type 'list'>(3, 6)(3, 6)(3, 6)(3, 6)(3, 6)Process finished with exit code 0


原创粉丝点击