python中创建numpy数组的方法有哪些?
python中创建numpy数组的方法有哪些?
python中array模块提供一维数组的操作,但是二维数组难易操作,而numpy数组的创建可以满足多维数组的各种运算。本文介绍python中创建numpy数组的三种方法:1、通过python的列表来构建;2、通过python的元组创建一个数组;3、使用array方法构造。
方法一:通过python的列表来构建
deftest1():
#通过python的list来构建numpyarray
list1=[[1,2,3]]
list2=[[1],[2],[3]]
方法二:通过python的元组创建一个数组
arr=np.array((1,3,2))
print("\nArraycreatedusing"
"passedtuple:\n",arr)
方法三:使用array方法构造
>>>x=np.array([2,3,1,0])
>>>x=np.array([2,3,1,0])
>>>x=np.array([[1,2.0],[0,0],(1+1j,3.)])#notemixoftupleandlists,
andtypes
>>>x=np.array([[1.+0.j,2.+0.j],[0.+0.j,0.+0.j],[1.+1.j,3.+0.j]])
以上就是python中创建numpy数组的三种方法,大家选择其中一种方法使用就可以啦~更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python包的导入方式有几种
python包的导入方式有几种本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、from...import导入frompackageimportmodule1,mo详情>>
2023-11-08 23:58:44
python参数是什么
python参数是什么1、概念Python中函数的参数是不变的对象。Python函数具有灵活的参数形式,但默认参数必须指向不变的对象。如果默认参数是可变...详情>>
2023-11-08 19:48:30
python使用items()遍历键值对
python使用items()遍历键值对字典可以用来存储各种方式的信息,所以有很多方式可以通过字典的所有键值对、键或值。说明1、即使通过字典,键值对...详情>>
2023-11-08 19:23:48
python按行读取文件的方法比较
python按行读取文件的方法比较1、read方法默认会把文件的所有内容一次性读取到内存。如果文件太大,对内存的占用会非常严重2、readline方法,re...详情>>
2023-11-08 18:22:27