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定时检查某个进程是否已经关闭的方法
May 20 Python
Python中使用urllib2模块编写爬虫的简单上手示例
Jan 20 Python
Python竟能画这么漂亮的花,帅呆了(代码分享)
Nov 15 Python
Python使用Django实现博客系统完整版
Sep 29 Python
详解windows python3.7安装numpy问题的解决方法
Aug 13 Python
解决python selenium3启动不了firefox的问题
Oct 13 Python
python pandas实现excel转为html格式的方法
Oct 23 Python
TensorFlow 多元函数的极值实例
Feb 10 Python
Python编程快速上手——Excel到CSV的转换程序案例分析
Feb 28 Python
python openCV实现摄像头获取人脸图片
Aug 20 Python
详解torch.Tensor的4种乘法
Sep 03 Python
详解python对象之间的交互
Sep 29 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
php注册和登录界面的实现案例(推荐)
2016/10/24 PHP
php curl批处理实现可控并发异步操作示例
2018/05/09 PHP
让你的PHP,APACHE,NGINX支持大文件上传
2021/03/09 PHP
[对联广告] JS脚本类
2006/08/27 Javascript
js中通过split函数分割字符串成数组小例子
2013/09/21 Javascript
js下拉菜单语言选项简单实现
2013/09/23 Javascript
js日期对象兼容性的处理方法
2014/01/28 Javascript
用JavaScript实现PHP的urlencode与urldecode函数
2015/08/13 Javascript
js replace(a,b)之替换字符串中所有指定字符的方法
2016/08/17 Javascript
AngularJS入门教程之双向绑定详解
2016/08/18 Javascript
Node.js 数据加密传输浅析
2016/11/16 Javascript
jquery实现图片列表鼠标移入微动
2016/12/01 Javascript
jQuery读取XML文件的方法示例
2017/02/03 Javascript
Javascript刷新页面的实例
2017/09/23 Javascript
PWA介绍及快速上手搭建一个PWA应用的方法
2019/01/27 Javascript
详解Vue组件之间通信的七种方式
2019/04/14 Javascript
vue项目使用高德地图的定位及关键字搜索功能的实例代码(踩坑经验)
2020/03/07 Javascript
微信小程序自定义yPicker组件实现省市区三级联动功能
2020/10/29 Javascript
[47:26]完美世界DOTA2联赛 LBZS vs Forest 第二场 11.07
2020/11/09 DOTA
python基础教程之数字处理(math)模块详解
2014/03/25 Python
Python中的高级函数map/reduce使用实例
2015/04/13 Python
Python自动化部署工具Fabric的简单上手指南
2016/04/19 Python
Python实现读取字符串按列分配后按行输出示例
2018/04/17 Python
基于numpy中数组元素的切片复制方法
2018/11/15 Python
网页布局中CSS样式无效的十个重要原因详解
2017/08/10 HTML / CSS
Sandro法国官网:法国成衣品牌
2019/08/28 全球购物
阿联酋彩妆品牌:OUD MILANO
2019/10/06 全球购物
自荐书封面下载
2013/11/29 职场文书
校企合作协议书
2014/04/16 职场文书
关于晚自习早退的检讨书
2014/09/13 职场文书
工厂标语大全
2014/10/06 职场文书
清明节网上祭英烈寄语2015
2015/03/04 职场文书
教师调动申请报告
2015/05/18 职场文书
困难补助申请报告
2015/05/19 职场文书
浅谈Java实现分布式事务的三种方案
2021/06/11 Java/Android
springboot拦截器无法注入redisTemplate的解决方法
2021/06/27 Java/Android