1. 在python学习的过程中,经常要创建数组,对数组里面的元素进行操作,所以我们需要掌握python中对于数组操作的基本语法
2. 下面是一些常用的数组创建、赋值的一些语法:
其中需要使用到python中的numpy包,我们使用对数组操作的相关函数需要导入这个包
import numpy as np
numpy中的array方法(构造器方法):
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
| 序号 | 参数及描述 |
|---|---|
| 1. | object 任何暴露数组接口方法的对象都会返回一个数组或任何(嵌套)序列。 |
| 2. | dtype 数组的所需数据类型,可选。 |
| 3. | copy 可选,默认为true,对象是否被复制。 |
| 4. | order C(按行)、F(按列)或A(任意,默认)。 |
| 5. | subok 默认情况下,返回的数组被强制为基类数组。 如果为true,则返回子类。 |
| 6. | ndmin 指定返回数组的最小维数 |
① 创建指定维度和数据类型的数组:
a)numpy.array(参数列表)
b)调整数组的大小:ndarray.shape(参数列表),这一数组属性返回一个包含数组维度的元组,此外reshape函数也可以用来调整数组大小,用法与shape方法类似
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 6 09:39:44 2019
"""
import numpy as np
#numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
print('创建一维数组: ')
a = np.array([1, 2, 3])
#输完一行数组数据之后换行
print(a, '\n')
print('创建二维数组: ')
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a, '\n')
#最小维度
a = np.array([1, 2, 3, 4, 5], ndmin = 2)
print(a, '\n')
#dataType数据类型 complex表示的是复数
a = np.array([1, 2, 3], dtype = np.float32)
print(a, '\n')
#调整数组的大小: ndarray.shape
#这一数组属性返回一个包含数组维度的元组,它也可以用于调整数组大小
print('调整数组大小: ')
a = np.array([[1, 2, 3], [4, 5, 6]])
a.shape = (3, 2)
print(a, "\n")
#NumPy 也提供了reshape函数来调整数组大小
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.reshape(3, 2))
② 创建初始化数组:
a)numpy.empty:它创建指定形状和dtype的未初始化数组。 它使用以下构造函数:
numpy.empty(shape, dtype = float, order = 'C')
构造器接受下列参数:
| 序号 | 参数及描述 |
|---|---|
| 1. | Shape 空数组的形状,整数或整数元组 |
| 2. | Dtype 所需的输出数组类型,可选 |
| 3. | Order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组 |
b)numpy.zeros:返回特定大小,以 0 填充的新数组
numpy.zeros(shape, dtype = float, order = 'C')
构造器接受下列参数:
| 序号 | 参数及描述 |
|---|---|
| 1. | Shape 空数组的形状,整数或整数元组 |
| 2. | Dtype 所需的输出数组类型,可选 |
| 3. | Order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组 |
c)numpy.ones:返回特定大小,以 1 填充的新数组
numpy.ones(shape, dtype = None, order = 'C')
构造器接受下列参数:
| 序号 | 参数及描述 |
|---|---|
| 1. | Shape 空数组的形状,整数或整数元组 |
| 2. | Dtype 所需的输出数组类型,可选 |
| 3. | Order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组 |
d)numpy.asarray:此函数类似于numpy.array,除了它有较少的参数。 这个例程对于将 Python 序列转换为ndarray非常有用
numpy.asarray(a, dtype = None, order = None)
构造器接受下列参数:
| 序号 | 参数及描述 |
|---|---|
| 1. | a 任意形式的输入参数,比如列表、列表的元组、元组、元组的元组、元组的列表 |
| 2. | dtype 通常,输入数据的类型会应用到返回的ndarray |
| 3. | order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组 |
具体代码如下:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 6 10:34:57 2019
"""
import numpy as np
#创建一个未初始化的数组所以数组里面的值是随机的
#Order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组
#dtype为返回的数组的数据类型
arr = np.empty([7], dtype = int, order = 'C')
print(arr, '\n')
arr = np.empty([3, 2], dtype = int)
print(arr, '\n')
arr = np.zeros(10, dtype = int)
print(arr, '\n')
arr = np.ones(7, dtype = int)
print(arr, '\n')
arr = np.ones([2, 2], dtype = int)
print(arr, '\n')
arr = np.asarray([12, 34, 56, 78], dtype = int)
print(arr, '\n')
arr = np.asarray(['12', '34','56', '8'], dtype = int)
print(arr, '\n')
arr = np.asarray((1, 2, 3, 4), dtype = int)
print(arr, '\n')
③ numpy.arange:这个函数返回ndarray对象,包含给定范围内的等间隔值
numpy.arange(start, stop, step, dtype)
构造器接受下列参数:
| 序号 | 参数及描述 |
|---|---|
| 1. | start 范围的起始值,默认为0 |
| 2. | stop 范围的终止值(不包含) |
| 3. | step 两个值的间隔,默认为1 |
| 4. | dtype 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型 |
具体的代码如下:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 6 10:39:37 2019
"""
import numpy as np
#numpy.arange: numpy.arange(start, stop, step, dtype)
arr = np.arange(1, 8, 2, int)
print(arr, "\n")
本文介绍了Python中使用numpy库创建和操作数组的方法,包括创建指定维度的数组、初始化数组(如zeros、ones等)、调整数组大小及使用arange生成等间距数值序列。

被折叠的 条评论
为什么被折叠?



