日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python怎样定义一个数组_python如何建立全零数组

發布時間:2023/12/10 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python怎样定义一个数组_python如何建立全零数组 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

語句格式:

numpy.zeros(shape, dtype=float, order='C')

參數說明:

shape:整型或元素為整型的序列,表示生成的新數組的shape,如(2,3)或 2。

dtype:生成數組的數據格式,如numpy.int8。默認為numpy.float64。

order:{'C', 'F'}可選,是否將多維數據存儲為C-或Fortran-contiguous(按行或按列)順序。

返回值:ndarray,一個指定了shape, dtype, order的零數組。

示例見下:

第四個例子看起來很方便。

Numpy文檔原文:

numpy.zeros

numpy.zeros(shape, dtype=float, order='C')

Return a new array of given shape and type, filled with zeros.

Parameters:

shape : int or sequence of ints

Shape of the new array, e.g., (2, 3) or 2.

dtype : data-type, optional

The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.

order : {‘C', ‘F'}, optional

Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory.

Returns:

out : ndarray

Array of zeros with the given shape, dtype, and order.

#指定長度的一維數組

>>> np.zeros(5)

array([ 0., 0., 0., 0., 0.])

#指定數據類型,指定長度的一維數組

>>> np.zeros((5,), dtype=int)

array([0, 0, 0, 0, 0])

#二維數組

>>> np.zeros((2, 1))

array([[ 0.],

[ 0.]])

>>> s = (2,2)

>>> np.zeros(s)

array([[ 0., 0.],

[ 0., 0.]])

#指定dtype

>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype

array([(0, 0), (0, 0)],

dtype=[('x', '

內容擴展:

python創建數組的方法

直接定義法:

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-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

到此這篇關于python如何建立全零數組的文章就介紹到這了,更多相關python建立全零數組的方法內容請搜索我們以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持我們!

本文標題: python如何建立全零數組

本文地址: http://www.cppcns.com/jiaoben/python/325580.html

總結

以上是生活随笔為你收集整理的python怎样定义一个数组_python如何建立全零数组的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。