python如何建立全零数组


Posted in Python onJuly 19, 2020

语句格式:

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', '<i4'), ('y', '<i4')])

内容扩展:

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 相关文章推荐
Python的Tornado框架的异步任务与AsyncHTTPClient
Jun 27 Python
在Linux命令行终端中使用python的简单方法(推荐)
Jan 23 Python
python中int与str互转方法
Jul 02 Python
一个可以套路别人的python小程序实例代码
Apr 09 Python
Python2和3字符编码的区别知识点整理
Aug 08 Python
详解Python绘图Turtle库
Oct 12 Python
python实现百度OCR图片识别过程解析
Jan 17 Python
Python环境使用OpenCV检测人脸实现教程
Oct 19 Python
基于Python实现全自动下载抖音视频
Nov 06 Python
sublime3之内网安装python插件Anaconda的流程
Nov 10 Python
Python 线程池模块之多线程操作代码
May 20 Python
一行Python命令实现批量加水印
Apr 07 Python
解决python中0x80072ee2错误的方法
Jul 19 #Python
python给视频添加背景音乐并改变音量的具体方法
Jul 19 #Python
python中加背景音乐如何操作
Jul 19 #Python
python实现最短路径的实例方法
Jul 19 #Python
python等待10秒执行下一命令的方法
Jul 19 #Python
python怎么删除缓存文件
Jul 19 #Python
python实现从ftp上下载文件的实例方法
Jul 19 #Python
You might like
Zerg基本策略
2020/03/14 星际争霸
PHP 5.0对象模型深度探索之对象复制
2008/03/27 PHP
PHP 引用是个坏习惯
2010/03/12 PHP
PHP开发环境配置(MySQL数据库安装图文教程)
2010/04/28 PHP
用Simple Excel导出xls实现方法
2012/12/06 PHP
浅析PHP程序设计中的MVC编程思想
2014/07/28 PHP
PHP正则判断一个变量是否为正整数的方法
2019/02/27 PHP
js实现DIV的一些简单控制
2007/06/04 Javascript
jquery实现商品拖动选择效果代码(自写)
2013/05/28 Javascript
javascript实现一个数值加法函数
2015/06/26 Javascript
Vue.js 表单校验插件
2016/08/14 Javascript
javascript定时器取消定时器及优化方法
2017/07/08 Javascript
详解vue axios中文文档
2017/09/12 Javascript
prototype.js简单实现ajax功能示例
2017/10/18 Javascript
js实现AI五子棋人机大战
2020/05/28 Javascript
js实现div色块拖动录制
2020/01/16 Javascript
vue实现在线学生录入系统
2020/05/30 Javascript
jQuery实现二级导航菜单的示例
2020/09/30 jQuery
Python常见格式化字符串方法小结【百分号与format方法】
2016/09/18 Python
Python中的类与类型示例详解
2019/07/10 Python
Python django框架输入汉字,数字,字符生成二维码实现详解
2019/09/24 Python
Spring实战之使用util:命名空间简化配置操作示例
2019/12/09 Python
python time.strptime格式化实例详解
2021/02/03 Python
canvas实现飞机打怪兽射击小游戏的示例代码
2018/07/09 HTML / CSS
C++是不是类型安全的
2014/02/18 面试题
什么是ESB?请介绍一下ESB?
2015/05/27 面试题
实习生单位鉴定意见
2013/12/04 职场文书
大学生职业生涯规划书的基本内容
2014/01/06 职场文书
正科级干部考察材料
2014/05/29 职场文书
2014年化验室工作总结
2014/11/21 职场文书
超市工作总结范文2014
2014/12/19 职场文书
公务员个人总结
2015/02/12 职场文书
幼儿园小班个人工作总结
2015/02/12 职场文书
2016秋季运动会开幕词
2016/03/04 职场文书
一文搞懂redux在react中的初步用法
2021/06/09 Javascript
千万级用户系统SQL调优实战分享
2022/03/03 MySQL