Python3 数组

本文详细介绍了Python3中Numpy数组的使用,包括list与array的区别、创建数组的各种方法,如numpy.empty、numpy.zeros、numpy.ones、numpy.asarray等,并讲解了数组的操作,如reshape、flat、flatten、ravel和transpose等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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]]

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值