Python常用的数组操作

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")

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值