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批量检查网站的可用性
Sep 09 Python
Python引用类型和值类型的区别与使用解析
Oct 17 Python
PyQt5每天必学之布局管理
Apr 19 Python
python读写配置文件操作示例
Jul 03 Python
利用python在大量数据文件下删除某一行的例子
Aug 21 Python
python使用socket 先读取长度,在读取报文内容示例
Sep 26 Python
使用tqdm显示Python代码执行进度功能
Dec 08 Python
Django 自定义404 500等错误页面的实现
Mar 08 Python
安装多个版本的TensorFlow的方法步骤
Apr 21 Python
Scrapy模拟登录赶集网的实现代码
Jul 07 Python
python rsa-oaep加密的示例代码
Sep 23 Python
Python排序算法之插入排序及其优化方案详解
Jun 11 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加密解密的代码
2007/07/16 PHP
zend framework配置操作数据库实例分析
2012/12/06 PHP
php 创建以UNIX时间戳命名的文件夹(示例代码)
2014/03/08 PHP
php+jQuery.uploadify实现文件上传教程
2014/12/26 PHP
Symfony数据校验方法实例分析
2015/01/26 PHP
24条货真价实的PHP代码优化技巧
2016/07/28 PHP
PHP数组常用函数实例小结
2018/08/20 PHP
javascript new一个对象的实质
2010/01/07 Javascript
window.location.hash 属性使用说明
2010/03/20 Javascript
javascript cookies 设置、读取、删除实例代码
2010/04/12 Javascript
JavaScript中的索引数组、关联数组和静态数组、动态数组讲解
2014/11/08 Javascript
js实现StringBuffer的简单实例
2016/09/02 Javascript
Asp.Net之JS生成分页条的方法
2016/11/23 Javascript
详解JavaScript模块化开发
2016/12/04 Javascript
js与jQuery实现的用户注册协议倒计时功能实例【三种方法】
2017/11/09 jQuery
实例教学如何写vue插件
2017/11/30 Javascript
Angular17之Angular自定义指令详解
2018/01/21 Javascript
使用 Node.js 实现图片的动态裁切及算法实例代码详解
2018/09/29 Javascript
使用Vue.observable()进行状态管理的实例代码详解
2019/05/26 Javascript
[00:35]TI7不朽珍藏III——寒冰飞龙不朽展示
2017/07/15 DOTA
[01:30]我们共输赢 完美世界城市挑战赛开启全新赛季
2019/04/19 DOTA
python sort、sorted高级排序技巧
2014/11/21 Python
Python 冒泡,选择,插入排序使用实例
2015/02/05 Python
python+pyqt实现右下角弹出框
2017/10/26 Python
Django基础知识与基本应用入门教程
2018/07/20 Python
Python增强赋值和共享引用注意事项小结
2019/05/28 Python
python join方法使用详解
2019/07/30 Python
Django文件存储 默认存储系统解析
2019/08/02 Python
django基础学习之send_mail功能
2019/08/07 Python
Python实现的北京积分落户数据分析示例
2020/03/27 Python
IFCHIC台湾:欧美国际设计师品牌
2019/05/18 全球购物
品牌宣传方案
2014/03/21 职场文书
简单租房协议书
2014/04/09 职场文书
2014最新自愿离婚协议书范本
2014/11/19 职场文书
玄武湖导游词
2015/02/05 职场文书
netty 实现tomcat的示例代码
2022/06/05 Servers