数组的相关知识(python实现数独自动算法之四)

在代码实现的过程中可能要用到数组,这个以前没接触过,先扫下盲,都是其他大神的结晶,搬过来备忘。
在NumPy中维度(dimensions)叫做轴(axes),轴的个数叫做秩(rank)。
numpy库数组属性查看:类型、尺寸、形状、维度

import numpy as np
q = np.array([1,2,3,4],dtype=np.complex128)
print(“数据类型”,type(q)) #打印数组数据类型
print(“数组元素数据类型:”,q.dtype) #打印数组元素数据类型
print(“数组元素总数:”,q.size) #打印数组尺寸,即数组元素总数
print(“数组形状:”,q.shape) #打印数组形状
print(“数组的维度数目”,q.ndim) #打印数组的维度数目

如上内容转自:
https://www.cnblogs.com/llfctt/p/12035080.html

numpy列表与数组的相互转换

1、 转成数组的matrix对象,使用np.mat()方法。
a = [[2,3,4],[4,7,1]] np.mat(a)
matrix([[2, 3, 4],
[4, 7, 1]])
2、 转成数组的ndarray对象,使用np.array()方法。
a = [[2,3,4],[4,7,1]]
np.array(a)
array([[2, 3, 4],
[4, 7, 1]])

• 数组转列表
不管是matrix对象还是ndarray对象,都可以使用object.tolist()方法转为成列表。
a = np.mat(((1,2),(2,3)))
a.tolist()
Out[2]: [[1, 2], [2, 3]]
b = np.array(((1,2),(2,3)))
b.tolist()
Out[4]: [[1, 2], [2, 3]]
如上内容转自:
https://blog.csdn.net/wzyaiwl/article/details/90575627

列表推导式找出指定元素的索引

一维数组:
from nmupy import *
y=array([4,1,6,2])
[x for x in range(y.shape[0]) if y[x]>3]
输出:[0,2] 即第0,2位置数满足条件。

二维数组:
from numpy import *
y=arange(9).reshpae(3,3) #生成三行三列的二维数组如下:
y
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
where(y==4) #用where查找,并返回索引
(array([1], dtype=int64), array([1], dtype=int64))

4位于第二行第二列。

总结Numpy中matrix和ndarray的区别(看不太懂)

该内容详见:
https://blog.csdn.net/wzyaiwl/article/details/90552935

Numpy的详细教程

该内容详见:
https://zhuanlan.zhihu.com/p/24988491

猜你喜欢

转载自blog.csdn.net/kim5659/article/details/108667035