admin 管理员组

文章数量: 1184232


2024年4月21日发(作者:dirent结构体)

Python实现数组的创建

在Python中,可以使用多种方式创建数组。以下是几种常见的创建数组的

方法:

1. 使用列表创建数组:

import numpy as np

arr1 = ([1, 2, 3, 4, 5]) # 从列表创建一维数组

arr2 = ([[1, 2, 3], [4, 5, 6]]) # 从嵌套列表创建二维数组

2. 使用`arange`函数创建连续的数组:

import numpy as np

arr3 = (1, 10, 2) # 创建一个从1到9的数组,步长为2

3. 使用`zeros`函数创建全零数组:

import numpy as np

arr4 = ((3, 4)) # 创建一个3x4的全零数组

4. 使用`ones`函数创建全一数组:

import numpy as np

arr5 = ((2, 3, 4)) # 创建一个2x3x4的全一数组

5. 使用`random`函数创建随机数组:

import numpy as np

arr6 = (2, 3) # 创建一个2x3的随机数组,元素在0到

1之间

这些只是创建数组的一些常见方法,NumPy还提供了其他更多的函数和选

项,可以根据需要选择合适的方式来创建数组。


本文标签: 数组 创建 函数 使用