在NumPy中返回具有给定形状和类型,并填充填充值的新数组
要在Python NumPy中返回具有给定形状和类型,并填充填充值的新数组,请使用**numpy.full()**方法。第一个参数是新数组的形状。第二个参数设置填充值。
dtype是数组所需的數據類型。order建议是否以C或Fortran连续(行或列方式)顺序在内存中存储多维数据。
NumPy提供全面的数学函数、随机数生成器、线性代数例程、傅里叶变换等等。它支持广泛的硬件和计算平台,并且与分布式、GPU和稀疏数组库配合良好。
步骤
首先,导入所需的库:
import numpy as np
要在Python NumPy中返回具有给定形状和类型,并填充填充值的新数组,请使用numpy.full()方法。第二个参数设置填充值:
arr = np.full((4,5), fill_value = 999)
显示数组:
print("Array...
",arr)获取数据类型:
print("
Array datatype...
",arr.dtype)
获取数组的维度:
print("
Array Dimensions...
",arr.ndim)获取数组的形状:
print("
Our Array Shape...
",arr.shape)
获取数组的元素个数:
print("
Elements in the Array...
",arr.size)示例
import numpy as np
# To return a new array of given shape and type, filled with a fill value, use the numpy.full() method in Python Numpy
# The 1st parameter is the shape of the new array
# The 2nd parameter sets the fill value
arr = np.full((4,5), fill_value = 999)
# Displaying our array
print("Array...
",arr)
# Get the datatype
print("
Array datatype...
",arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr.size)输出
Array... [[999 999 999 999 999] [999 999 999 999 999] [999 999 999 999 999] [999 999 999 999 999]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (4, 5) Elements in the Array... 20
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP