python定义数组array |
您所在的位置:网站首页 › python中的数组怎么定义 › python定义数组array |
一 直接定义法: 1.直接定义matrix=[0,1,2,3] 2.间接定义matrix=[0 for i in range(4)] print(matrix) 二 Numpy方法: Numpy内置了从头开始创建数组的函数: zeros(shape)将创建一个用指定形状用0填充的数组。默认的dtype是float64。 下面是几种常用的创建方法:#coding=utf-8import numpy as np a = np.array([1,2,3,4,5])print a b = np.zeros((2,3))print b c = np.arange(10)print c d = np.arange(2,10,dtype=np.float)print d e = np.linspace(1.0,4.0,6)print e f = np.indices((3,3))print f 三 其他转换法: 数组还有比较常用的一种方法,就是从其他Python结构(例如,列表,元组)转换。 下面给出一些例子。 列表转数组:a = [] a.append((1,2,4)) a.append((2,3,4)) a = np.array(a) a.flatten() 元组转成数组:import numpy as np mylist = [1,2,3]print tuple(mylist) iarray = np.array(tuple(mylist))print iarray 相关推荐:《Python教程》 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |