Python 2D 数组:二维列表示例

⚡ 智能摘要

Python 二维数组是指将一个数组嵌套在另一个数组中,形成类似表格的行和列。以下示例展示了如何创建、访问、插入、更新、删除和调整二维数组的大小。

  • 🧱 结构体: 二维数组是由行和列组成的数组,其中包含多个数组。
  • 🔢 索引: 使用 array[row][column] 访问元素;索引从 0 开始。
  • 插入和更新: 使用 insert() 添加行,并按索引更改值。
  • 🗑️ 删除和大小: 使用 del 删除行,使用 len() 计算行数。
  • 🔁 迭代: 使用嵌套的 for 循环遍历行和列。
  • 🧮 NumPy选项: 对于繁重的数值运算,为了提高速度,可以使用 numpy.array() 创建真正的二维数组。

Python 二维阵列

什么是二维数组? Python?

数组是一种用于存储元素的数据结构。数组只能存储相似类型的元素。二维数组定义为数组内的数组。数组的索引从 0 开始,以数组大小减 1 结束。我们可以在数组中创建“n”个数组。

Python 2D 数组

在上图中,我们可以看到索引唯一地标识每个数组元素。

如何在 Python?

我们可以创建一个有行和列的二维数组(列表)。

句法:

[[r1,r2,r3,..,rn],[c1,c2,c3,.......,cn]]

哪里,

r 代表行,c 代表列

计费示例: 以下是创建的示例

具有 2 行 4 列的二维数组

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]
#display
print(array)

输出:

[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10]]

访问值

我们可以使用索引位置访问值

句法:

我们可以使用以下方法获取行值 [] 操作者

array[row index]

我们可以使用获取列值 [][]

Array[row index][column index]

哪里,

  • array 是输入数组
  • row index 是行索引位置,从 0 开始
  • 列索引是一行中从0开始的列索引位置。

计费示例:

在此示例中,我们将使用索引位置访问值

#creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]

#display
print(array)

#get the first row
print(array[0])

#get the third row
print(array[2])

#get the first row third element
print(array[0][2])

#get the third row forth element
print(array[2][3])

输出:

[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10]]
[23, 45, 43, 23, 45]
[89, 90, 87, 65, 44]
43
65

我们还可以使用以下方式访问元素 for loop

句法:

for rows in the array:
  for columns in rows:
    print(columns)

哪里,

  • 行用于逐行迭代
  • columns 用于迭代每行中存在的值。

计费示例:

Creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]

#use for loop to iterate the array
for rows in array:
 for columns in rows:
   print(columns,end=" ")
   print()

输出:

23 45 43 23 45
45 67 54 32 45
89 90 87 65 44
23 45 67 32 10

将值插入二维数组

这里我们将使用 insert() 函数将值插入二维数组

语法:

array.insert(index,[values])

哪里,

  • 该数组是输入数组
  • 索引是插入特定行的行位置
  • value 是要插入到数组中的值

计费示例: 插入数组中的值

#Create 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]

#insert the row at 5 th position
array.insert(2, [1,2,3,4,5])

#insert the row at 6 th position
array.insert(2, [1,2,3,4,5])

#insert the row at 7 th position
array.insert(2, [1,2,3,4,5])

#display
print(array)

输出:

[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10]]

将值更新到二维数组中

这里有两种更新二维数组(列表)中的值的方法。

您可以使用以下语法来更新行

array[row_index]= [values]

您可以使用以下语法更新行内的列值

array[row_index][column_index]= [values]

计费示例:

#creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]

#update row values in the 3rd row
array[2]=[0,3,5,6,7]

#update row values in the 5th row
array[2]=[0,3,5,6,7]

#update the first row , third column
array[0][2]=100

#update the second row , third column
array[1][2]=400

#display
print(array)

输出:

[[23, 45, 100, 23, 45], [45, 67, 400, 32, 45], [0, 3, 5, 6, 7], [23, 45, 67, 32, 10]]

从二维数组中删除值

您可以使用 del function

语法:

del array[index]

哪里,

  • 该数组是输入数组
  • index 指的是行索引

计费示例:

#creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]

#delete row values in the 3rd row
del array[2]

#delete row values in the 2nd row
del array[1]

#display
print(array)

输出:

[[23, 45, 43, 23, 45], [23, 45, 67, 32, 10]]

获取二维数组的大小

您可以使用 line() 函数获取二维数组的大小。它将返回数组中的行数

句法:

len(array)

计费示例:

获取二维数组的长度

#creare 2D array with 4 rows and 5 columns
array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]
#display
print(len(array))

输出:

4

重要提示 Python 数组(列表)方法

以下是一些重要的 Array(list) 方法

付款方式 描述 句法 例如:
附加(): 此方法帮助我们在现有数组的末尾添加一个元素。 数组.append(值)
# Adding an element using append method to the end of an array array=[1,2,3] array.append(4) print(array)

输出:

[1,2,3,4]
清除(): 此方法帮助我们删除数组中存在的所有元素 数组.clear()
# Removing all the elements from an array array=[1,2,3]
array.clear()

输出:

[]
复制(): 此方法帮助我们将数组的内容复制到新数组 数组1=数组.复制()
#Copying the elements from an array to a new array array=[1,2,3] array1=[]
array1=array.copy()
  print(array1)

输出:

[1,2,3]
数数(): 此方法可以帮助我们计算数组中指定元素出现的次数 数组.count(元素)
#Counting the no of times an element is present in an array array=[1,2,3]
print(array.count(8))
 Output: 0
延长(): 此方法可以帮助我们将数组扩展到其组成元素之外。 数组.扩展(数组1)
#Extending an existing array with another array array=[1,2,3] array1=[4,5,6] array.extend(array1) print(array)
Output: [1,2,3,4,5,6]
指数(): 此方法帮助我们找到数组中元素的索引 数组.索引(元素)
#returing the index an element in an array array=[1,2,3]
print(array.index(3))

输出:

2
插入(): 此方法帮助我们将元素插入到指定索引的数组中。 数组.插入(索引,元素)
#Inserting an element at a specified index into an array array=[1,2,3]
array.insert(2,4)
  print(array)

输出:

[1,2,4,3]
流行音乐(): 此方法帮助我们删除指定索引处的元素 数组.pop(索引)
#Removing an element at specified index through pop method
array=[1,2,3]
array.pop(2)
  print(array)

输出:

[1,2]
消除(): 此方法帮助我们删除数组中特定的匹配元素。 数组.删除(元素)
array=[1,2,3]
array.remove(2)
  print(array)

输出:

[1,3]
Reverse(): 此方法的作用是反转数组中的元素。 数组.reverse()
array=[1,2,3,4]
array.reverse()
  print(array)

输出:

[4,3,2,1]

常见问题

In Python二维数组通常以列表的列表形式实现,因此这两个术语含义相同。为了实现快速数值运算,NumPy 库提供了一种专用的真正数组类型。

导入 NumPy,然后将列表的列表传递给 `numpy.array()`,例如 `np.array([[1,2],[3,4]])`。NumPy 数组支持快速的逐元素运算、切片和重新排序。ping 这是普通列表的列表无法高效完成的任务。

使用列表推导式,例如 [[0]*cols for _ in range(rows)]。避免使用 [[0]*cols]*rows,因为它重复了相同的内部列表引用,因此编辑一行会意外地更改所有行。

使用 `len(array)` 获取行数,使用 `len(array[0])` 获取第一行的列数。在 NumPy 中,`shape` 属性返回的这两个值都是一个 (rows, columns) 元组。

列表存储混合数据类型,并且分散在内存中。NumPy 数组则将单一数据类型存储在连续的内存中,占用空间更小,并且由于使用 C 语言执行数值运算,因此速度更快。

使用嵌套的 for 循环:外层循环遍历每一行,内层循环遍历该行内的每一列。这样就可以逐个位置读取或更新每个元素。

AI 助手可以生成二维数组代码、解释索引错误、编写嵌套循环,以及在列表的列表和 NumPy 数组之间进行转换,从而加快学习和日常数据任务的速度。

是的。GitHub Copilot 可以根据注释自动完成二维数组的创建、索引和遍历,而智能工具可以重构嵌套循环或自动检测文件中的差一错误。

总结一下这篇文章: