What are the rules for indexing in multi-dimensional ar…

In Python, the indexing rules for multidimensional arrays are as follows:

  1. The index of a multidimensional array starts from 0. The index of the first element is 0, the index of the second element is 1, and so on.
  2. The index range for each dimension goes from 0 to the length of that dimension minus 1.
  3. Access elements in a multi-dimensional array using square brackets ([]). Indexes within the brackets are separated by commas and represent indexes for different dimensions from left to right.

For example, for a two-dimensional array arr, you can access the element in the i-th row and j-th column using arr[i][j].

Here is an example code:

arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(arr[0][0])  # 输出1,访问第一行第一列的元素
print(arr[1][2])  # 输出6,访问第二行第三列的元素

Caution: When using multidimensional array indices, ensure that the indices do not exceed the array’s range, or else an IndexError will be raised.

bannerAds