[转载] Python3 数组

参考链接: Python中的Array | 数组1(简介和功能)

一、list和array的区别 

Python的数组通过Numpy包的array实现。 Python里二者最大的区别是,list可以存储不同类型的数据,而array只能存储相同类型的数据。 

import numpy

#直接定义

a = [1,2,3,4,'5']   #列表list,可以混合类型

b = numpy.array([1,2,3,4,5])        #数字数组array

c = numpy.array([1,2,3,4,'5'])      #字符数组array

 

#打印出来也有不同

print(a)    #[1, 2, 3, 4]

print(b)    #[1 2 3]

print(c)    #['1' '2' '3' '4' '5']

 

#生成值为连续数字的对象

a1 = list(range(5))

b1 = numpy.arange(5)

 

#打印结果

print(a1)   #[0, 1, 2, 3, 4]

print(b1)   #[0 1 2 3 4]

 

 

二、创建数组的方法 

(一) numpy.empty 创建未初始化的数组(非空,元素为随机值) 

numpy.empty(shape, dtype = float, order = ‘C’) 参数列表分别为形状,数据类型,在计算机中的存储为行优先 ‘C’ 或者列优先 ‘F’。 

import numpy 

x = numpy.empty([3,2], dtype = int) 

print(x)

 

[[0 0]

 [0 0]

 [0 0]]

 

(二) numpy.zeros 创建元素为 0 的数组 

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

import numpy

y = numpy.zeros((2,2), dtype=int)

print(y)

 

[[0 0]

 [0 0]]

 

(三) numpy.ones 创建元素为 1 的数组 

import numpy

z = numpy.ones((2,2))    #这几个创建方式都需要注意第一个参数的形式

print(z)

 

[[1. 1.] [1. 1.]] 

(四) numpy.asarray 根据已有的元组或者列表创建数组 

numpy.asarray(a, dtype = None, order = None) 这是从列表转换为数组的例子 

import numpy

x = [[1,2,3],[4,5,6]]   #需要注意原列表的形式

y = [[1,2,3],[4,5]]

z = [[1,2,3],[4,5,'6']]

q = [[1,2,3],[4,5,6]]

 

a = numpy.asarray(x)

b = numpy.asarray(y)

c = numpy.asarray(z)

d = numpy.asarray(q,dtype=float)

print(a)

print(b)

print(c)

print(d)

 

[[1 2 3]

 [4 5 6]]

[list([1, 2, 3]) list([4, 5])]

[['1' '2' '3']

 ['4' '5' '6']]

[[1. 2. 3.]

 [4. 5. 6.]]

 

(五) numpy.frombuffer 流形式读入转换为数组 

numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0) 细节太多不讨论了,需要的时候再看 

(六) numpy.fromiter 从可迭代对象创建数组,返回一维数组 

numpy.fromiter(iterable, dtype, count=-1) count为读取的数据数量,默认为-1,读取所有数据 

import numpy

x = range(5)

it = iter(x)

a = numpy.fromiter(x, dtype=float)

print(a)

 

[0. 1. 2. 3. 4.]

 

(七) numpy.arange 从数值范围创建数组 

numpy.arange(start, stop, step, dtype) 

import numpy

a = numpy.arange(5)

print(a)

 

[0 1 2 3 4]

 

(八) numpy.linspace 创建等差数列一维数组 

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) num为数量,endpoint为真时stop被包含在数列中,retstep为真时显示间距 

import numpy

a = numpy.linspace(0,100,11)

b = numpy.linspace(0,100,11,retstep =True, dtype=int)

print(a)

print(b)

 

[  0.  10.  20.  30.  40.  50.  60.  70.  80.  90. 100.]

(array([  0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100]), 10.0)

 

(九) numpy.logspace 创建等比数列一维数组 

numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None) 序列的起始值为:base**start (base为底的start次方) 序列的终止值为:base ** stop (base为底的stop次方), 如果endpoint为true,该值包含于数列中 base为log的底数 

import numpy

a = numpy.logspace(1, 4, 4, base=3, dtype = int)

b = numpy.logspace(1, 10, 10, base=2)

print(a)

print(b)

 

[ 3  9 27 81]

[   2.    4.    8.   16.   32.   64.  128.  256.  512. 1024.]

 

三、array的操作 

(一) reshape 整形 

import numpy

a = numpy.arange(6)

b = a.reshape(3,2)    #改变数组形状,参数是行数和列数,需要匹配原数组的元素数量否则报错

print(a)

print(b)

 

[0 1 2 3 4 5 6 7]

[[0 1]

 [2 3]

 [4 5]

 [6 7]]

 

(二) flat 数组迭代器 

import numpy

#一维数组可以直接for循环迭代

a = numpy.arange(6)

for x in a:

    print(x)

 

b = numpy.arange(6).reshape(3,2)    #二维数组

#flat迭代器

for x in b.flat:

    print(x)

 

#多重for循环,跟迭代器二者等效

for x in b: 

    for y in x: 

        print(y)

 

(三) flatten 深拷贝,同copy() 

ndarray.flatten(order=‘C’) order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘K’ – 元素在内存中的出现顺序。 

import numpy

a = numpy.arange(4)

b=a.copy()

c=a.flatten()

 

a[1] = 8

b[0] = 9

 

print(a)

print(b)

print(c)

 

[0 8 2 3]

[9 1 2 3]

[0 1 2 3]

 

(四) ravel 返回数组的视图,修改会影响原数组 

numpy.ravel(a, order=‘C’) order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘K’ – 元素在内存中的出现顺序。 

import numpy

a = numpy.arange(8).reshape(2,4)

b = a.ravel()

c = a.ravel(order = 'F')    # 就这个修改不会影响其他的

d = a.ravel(order = 'A')

e = a.ravel(order = 'K')

print(a)

print(b)

print(c)

print(d)

print(e)

 

[[0 1 2 3]

 [4 5 6 7]]

[0 1 2 3 4 5 6 7]

[0 4 1 5 2 6 3 7]

[0 1 2 3 4 5 6 7]

[0 1 2 3 4 5 6 7]

 

注意,修改order='F’模式的时候不会影响其他模式的序列,没有深究为何,用的时候再找。 

b[4] = 44

c[5] = 55

d[6] = 66

e[7] = 77

print(a)

print(b)

print(c)

print(d)

print(e)

 

[[ 0  1  2  3]

 [44  5 66 77]]

[ 0  1  2  3 44  5 66 77]

[ 0  4  1  5  2 55  3  7]

[ 0  1  2  3 44  5 66 77]

[ 0  1  2  3 44  5 66 77]

 

(五) transpose 等同于T,翻转数组行和列 

numpy.transpose(arr, axes) arr:要操作的数组 axes:整数列表,对应维度,通常所有维度都会对换。 

import numpy

a = numpy.arange(6).reshape(2,3)

b = a.transpose()

c = a.T     #注意写法

 

print(a)

print(b)

print(c)

 

[[0 1 2]

 [3 4 5]]

[[0 3]

 [1 4]

 [2 5]]

[[0 3]

 [1 4]

 [2 5]]

 

(六) 后面暂时略,以后补完

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540581.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

201671010128 2017-09-24《Java程序设计》之继承

1.继承的概念及理解: 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类。继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法&#xff…

紫外线的形式是什么?

紫外线:紫外线 (UV: Ultraviolet) UV is an abbreviation of Ultraviolet. In RO water purifiers, the bacteria or germs which are present in the water cannot get killed by reverse osmosis process but this process can banish the dissolved solids and i…

[js高手之路] html5 canvas系列教程 - 掌握画直线图形的常用API

我们接着上文[js高手之路] html5 canvas系列教程 - 认识canvas以及基本使用方法继续. 一、直线的绘制 cxt.moveTo( x1, y1 ): 将画笔移动到x1, y1这个点 cxt.lineTo( x2, y2 ):将画笔从起点开始画直线,一直画到终点坐标( x2, y2 ) cxt.stroke…

金矿问题

Description: 描述: This is a standard interview problem featured in interview coding rounds of Amazon, Flipkart. 这是亚马逊Flipkart的采访编码回合中的标准采访问题。 Problem statement: 问题陈述: Given a gold mine of n*m dimensions, e…

[转载] python中的数组类型及特点

参考链接: Python中的Array | 数组2(简介和功能) 名称 表示方法示例 是否有序 函数方法(增删等) 特点 List 类型表示:L L [Adam, 95.5, Lisa, 85] 有序 增加:(1)L.append(Paul),增加…

puppet

Puppet前期环境(网络、解析、yum源、NTP)在上一章节已经准备就绪,接下来我们就开始安装Puppet了,安装Puppet其实很简单,官方已经提供了yum源,只需要自己将所需要的安装包下载下来然后做成本地yum源即可使用…

[转载] 【数学问题】利用python求解表达式

参考链接: Python 变量 |表达式 |条件和函数 有时候我们会遇到一些很复杂的表达式,或者想要求解某个表达式,但是手动计算的话不但耗时还费精力,我们能不能利用计算机来帮助我们进行计算呢? 1…

cesium广告牌_公路广告牌

cesium广告牌Description: 描述: This is a standard dynamic programing problem of finding maximum profits with some constraints. This can be featured in any interview coding rounds. 这是在某些约束条件下找到最大利润的标准动态编程问题。 这可以在任何…

你和大牛差了啥

mmp。无时无刻不在想和大牛差在哪里了。别人为什么可以那么牛逼而你tmd那么菜!整个人顿时都颓废了。啥事儿不想干。后来想了想感情就是他比较黑吧。

[转载] python数组的使用

参考链接: Python中整数的最大可能值是多少? 原文地址为: python数组的使用 python数组的使用 python数组的使用 2010-07-28 17:17 1、Python的数组分三种类型: (1) list 普通的链表,初始化后可以通过特定方法…

scala中循环守卫_Scala中的循环

scala中循环守卫Scala中的循环 (Loops in Scala) In programming, many times a condition comes when we need to execute the same statement or block of code more than one time. It could be difficult to write the same code multiple times, so programing language d…

50个必备基础命令

1.tar创建一个新的tar文件$ tar cvf archive_name.tar dirname/解压tar文件$ tar xvf archive_name.tar查看tar文件$ tar tvf archive_name.tar2. grep在文件中查找字符串(不区分大小写)$ grep -i "the" demo_file输出成功匹配的行,以及该行之后的三行$ g…

NM的完整形式是什么?

NM:无消息 (NM: No Message) NM is an abbreviation of "No Message". NM是“无消息”的缩写。 It is an expression, which is commonly used in the Gmail platform. It is also written as N/M or n/m or *n/m*. It is written in the subject of the…

[转载] python中全局变量和局部变量解析

参考链接: Python中的全局变量和局部变量 python函数中可以访问全局变量但是不能给全局变量赋值,除非进行显式声明global a 比如定义了全局变量 a 在函数my_fun()中可以直接访问a的值,而不需要global全局变量申明。下图为上面代码运行输出 …

【iCore4 双核心板_FPGA】例程十六:基于双口RAM的ARM+FPGA数据存取实验

实验现象: 核心代码: int main(void) {/* USER CODE BEGIN 1 */int i;int address,data;char error_flag 0;char receive_data[50];char buffer[8];char *p;/* USER CODE END 1 *//* MCU Configuration-----------------------------------------------…

[转载] Python中TFTP的理解

参考链接: Python中的打包pack和拆包unpack参数 Num01–>TFTP协议介绍 TFTP(Trivial File Transfer Protocol,简单文件传输协议) 是TCP/IP协议族中的一个用来在客户端与服务器之间进行简单文件传输的协议 特点: 1,简单 2…

gn fast-gn_GN的完整形式是什么?

gn fast-gnGN:晚安 (GN: Good Night) GN is an abbreviation of "Good Night". GN是“ Good Night”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yahoo Messenge…

从零开始编写自己的C#框架(27)——什么是开发框架

前言 做为一个程序员,在开发的过程中会发现,有框架同无框架,做起事来是完全不同的概念,关系到开发的效率、程序的健壮、性能、团队协作、后续功能维护、扩展......等方方面面的事情。很多朋友在学习搭建自己的框架,很多…

[转载] Python 递归 深入理解递归 Python递归剖析,绝对让你看懂!

参考链接: Python | print()中的结束参数 目录 递归剖析 递归的两个过程 return 返回值 详解 递归思路二分法和递归尾递归递归练习题 递归剖析 递归真的很重要,之前学的时候,学的一知半解,以为真正了解,每次想到递归…

laravel 项目迁移_在Laravel迁移

laravel 项目迁移Before moving forward we need to know some facts about it, 在继续前进之前,我们需要了解一些事实, Resources: In these directories, we have already a js, lang, sass and view page. Where, sass and js file holf their uncom…