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

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

Python 2D Arrays

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

如何在 Python 中创建数组?

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

语法:

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

其中,

r 代表行,c 代表列

示例:以下是创建的示例

具有 4 行 5 列的二维数组

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 开始的行索引位置
  • column index 是行中从 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 循环 访问元素

语法:

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

其中,

  • rows 用于逐行迭代
  • 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])

其中,

  • array 是输入数组
  • index 是插入特定行的行位置
  • 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 函数删除行

语法

del array[index]

其中,

  • array 是输入数组
  • 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

摘要

这里有一些重要的数组(列表)方法

方法 描述 语法 示例
Append() 此方法有助于我们将元素添加到现有数组的末尾。 array.append(value)
# 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() 此方法有助于我们删除数组中的所有元素 array.clear()
# Removing all the elements from an array array=[1,2,3] 
array.clear()

输出

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

输出

[1,2,3]
Count() 此方法有助于我们计算数组中指定元素的出现次数 array.count(element)
#Counting the no of times an element is present in an array array=[1,2,3]
print(array.count(8))
 Output: 0
Extend() 此方法有助于我们将数组扩展到其现有元素之外。 array.extend(array1)
#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]
Index() 此方法有助于我们在数组中查找元素的索引 array.index(element)
#returing the index an element in an array array=[1,2,3] 
print(array.index(3))

输出

2
Insert() 此方法有助于我们将元素插入到数组的指定索引位置。 array.insert(index,element)
#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() 此方法有助于我们删除指定索引处的元素 array.pop(index)
#Removing an element at specified index through pop method
array=[1,2,3] 
array.pop(2) 
  print(array)

输出

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

输出

[1,3]
Reverse() 此方法有助于我们反转数组中的元素。 array.reverse()
array=[1,2,3,4] 
array.reverse() 
  print(array)

输出

[4,3,2,1]