Python 2D 数组:二维列表示例
数组是一种用于存储元素的数据结构。数组只能存储相似类型的元素。二维数组定义为数组内的数组。数组的索引从 0 开始,以数组大小减 1 结束。我们可以在数组中创建“n”个数组。
在上图中,我们可以看到索引唯一地标识每个数组元素。
如何在 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
总结
以下是一些重要的 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] |
Rev反之(): | 此方法的作用是反转数组中的元素。 | 数组.reverse() |
array=[1,2,3,4] array.reverse() print(array) 输出: [4,3,2,1] |