新聞中心
一、直接定義法:

成都創(chuàng)新互聯(lián),專注為中小企業(yè)提供官網(wǎng)建設(shè)、營(yíng)銷型網(wǎng)站制作、響應(yīng)式網(wǎng)站開發(fā)、展示型網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站等服務(wù),幫助中小企業(yè)通過網(wǎng)站體現(xiàn)價(jià)值、有效益。幫助企業(yè)快速建站、解決網(wǎng)站建設(shè)與網(wǎng)站營(yíng)銷推廣問題。
1.直接定義
matrix=[0,1,2,3]
2.間接定義
matrix=[0 for i in range(4)] print(matrix)
相關(guān)推薦:《python基礎(chǔ)教程》
二、Numpy方法:
Numpy內(nèi)置了從頭開始創(chuàng)建數(shù)組的函數(shù):zeros(shape)將創(chuàng)建一個(gè)用指定形狀用0填充的數(shù)組。默認(rèn)的dtype是float64。
下面是幾種常用的創(chuàng)建方法:
#coding=utf-8 import 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
三、其他轉(zhuǎn)換法:
數(shù)組還有比較常用的一種方法,就是從其他Python結(jié)構(gòu)(例如,列表,元組)轉(zhuǎn)換。
下面給出一些例子。
列表轉(zhuǎn)數(shù)組:
a = [] a.append((1,2,4)) a.append((2,3,4)) a = np.array(a) a.flatten()
元組轉(zhuǎn)成數(shù)組:
import numpy as np mylist = [1,2,3] print tuple(mylist) iarray = np.array(tuple(mylist)) print iarray
(推薦操作系統(tǒng):windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。)
網(wǎng)站題目:創(chuàng)新互聯(lián)Python教程:python怎么創(chuàng)建對(duì)象數(shù)組
當(dāng)前URL:http://m.5511xx.com/article/cogocho.html


咨詢
建站咨詢
