📜  如何在Python中引用数组中的元素

📅  最后修改于: 2022-05-13 01:55:27.634000             🧑  作者: Mango

如何在Python中引用数组中的元素

先决条件: Numpy

数组的元素可以像常规的Python数组一样被引用。由于Python内部不支持数组,这里每当我们使用术语数组时,我们指的是 python 列表,可用于构建任何所需维度的数组。除此之外,Python 的 NumPy 模块还提供了一个名为“array”的容器,用于存储数据集合。在本文中,我们将讨论如何引用Python数组中的元素以及 numpy Python中的数组。

  • 对于数组引用,只需将所需元素的索引传递给数组的名称。

句法:

array_name[index]
  • 为了使用 numpy 数组进行引用,首先使用 numpy 的数组函数创建一个数组,然后像常规数组一样引用它。

句法:

np.array([array elements])

下面针对各种情况给出了使用这两种方法的实现:

示例 1:引用一维数组中的项

Python数组示例

Python3
# Creating a array of elements
arr = [4, 6, 3, 9, 2]
  
# Referring elements of the array
# by index to a new variable
first_element = arr[0]
second_element = arr[1]
third_element = arr[2]
fourth_element = arr[3]
fifth_element = arr[4]
  
# Print the variables
print("First Element =", first_element)
print("Second Element =", second_element)
print("Third Element =", third_element)
print("Fourth Element =", fourth_element)
print("Fifth Element =", fifth_element)


Python3
# Importing numpy module
import numpy as np
  
# Creating a numpy array of elements
arr = np.array([4, 6, 3, 9, 2])
  
# Referring elements of the array
# by index to a new variable
first_element = arr[0]
second_element = arr[1]
third_element = arr[2]
fourth_element = arr[3]
fifth_element = arr[4]
  
# Print the variables
print("First Element =", first_element)
print("Second Element =", second_element)
print("Third Element =", third_element)
print("Fourth Element =", fourth_element)
print("Fifth Element =", fifth_element)


Python3
# Creating a 2d-array of elements
arr = [[4, 6, 3],
       [5, 9, 2],
       [1, 8, 7]]
  
# Referring elements of the 2d-array
# by row and column index to new variables
first_row_second_column = arr[0][1]
second_row_first_column = arr[1][0]
third_row_third_column = arr[2][2]
  
# Print the variables
print("First Row Second Column =", first_row_second_column)
print("Second Row First Column =", second_row_first_column)
print("Third Row Third Column =", third_row_third_column)


Python3
# Importing numpy module
import numpy as np
  
# Creating a 2d-numpy-array of elements
arr = np.array([[4, 6, 3],
                [5, 9, 2],
                [1, 8, 7]])
  
# Referring elements of the 2d-array
# by row and column index to new variables
first_row_second_column = arr[0][1]
second_row_first_column = arr[1][0]
third_row_third_column = arr[2][2]
  
# Print the variables
print("First Row Second Column =", first_row_second_column)
print("Second Row First Column =", second_row_first_column)
print("Third Row Third Column =", third_row_third_column)


Python3
# Creating a 3d-array of elements
arr = [[[4, 6, 3], [2, 6, 8], [3, 5, 12]],
       [[32, 11, 4], [23, 53, 89], [19, 17, 10]],
       [[14, 22, 52], [56, 43, 99], [20, 37, 32]]]
  
# Referring elements of the 3d-array
# by 3d index to new variables
first_second_second = arr[0][1][1]
second_first_third = arr[1][0][2]
third_third_first = arr[2][2][0]
  
# Print the variables
print("First Second Second Value =", first_second_second)
print("Second First Third Value =", second_first_third)
print("Third Third First Value =", third_third_first)


Python3
# Importing numpy module
import numpy as np
  
# Creating a 3d-array of elements
arr = np.array([[[4, 6, 3], [2, 6, 8], [3, 5, 12]],
                [[32, 11, 4], [23, 53, 89], [19, 17, 10]],
                [[14, 22, 52], [56, 43, 99], [20, 37, 32]]])
  
# Referring elements of the 3d-array
# by 3d index to new variables
first_second_second = arr[0][1][1]
second_first_third = arr[1][0][2]
third_third_first = arr[2][2][0]
  
# Print the variables
print("First Second Second Value =", first_second_second)
print("Second First Third Value =", second_first_third)
print("Third Third First Value =", third_third_first)


Python3
# Creating a 2d-array of elements
arr = [[4, 6, 3],
       [5, 9, 2],
       [1, 8, 7]]
  
# Referring rows of the 2d-array
# by row index to new variables
first_row = arr[0]
second_row = arr[1]
third_row = arr[2]
  
# Print the variables
print("First Row =", first_row)
print("Second Row =", second_row)
print("Third Row =", third_row)


Python3
# Importing numpy module
import numpy as np
  
# Creating a 2d-numpy-array of elements
arr = np.array([[4, 6, 3],
                [5, 9, 2],
                [1, 8, 7]])
  
# Referring rows of the 2d-array
# by row index to new variables
first_row = arr[0]
second_row = arr[1]
third_row = arr[2]
  
# Print the variables
print("First Row =", first_row)
print("Second Row =", second_row)
print("Third Row =", third_row)


输出:

Python 的 numpy 模块数组示例

蟒蛇3

# Importing numpy module
import numpy as np
  
# Creating a numpy array of elements
arr = np.array([4, 6, 3, 9, 2])
  
# Referring elements of the array
# by index to a new variable
first_element = arr[0]
second_element = arr[1]
third_element = arr[2]
fourth_element = arr[3]
fifth_element = arr[4]
  
# Print the variables
print("First Element =", first_element)
print("Second Element =", second_element)
print("Third Element =", third_element)
print("Fourth Element =", fourth_element)
print("Fifth Element =", fifth_element)

输出:

示例 2:引用二维数组中的项

Python数组示例

蟒蛇3

# Creating a 2d-array of elements
arr = [[4, 6, 3],
       [5, 9, 2],
       [1, 8, 7]]
  
# Referring elements of the 2d-array
# by row and column index to new variables
first_row_second_column = arr[0][1]
second_row_first_column = arr[1][0]
third_row_third_column = arr[2][2]
  
# Print the variables
print("First Row Second Column =", first_row_second_column)
print("Second Row First Column =", second_row_first_column)
print("Third Row Third Column =", third_row_third_column)

输出:

Python 的 numpy 模块数组示例

蟒蛇3

# Importing numpy module
import numpy as np
  
# Creating a 2d-numpy-array of elements
arr = np.array([[4, 6, 3],
                [5, 9, 2],
                [1, 8, 7]])
  
# Referring elements of the 2d-array
# by row and column index to new variables
first_row_second_column = arr[0][1]
second_row_first_column = arr[1][0]
third_row_third_column = arr[2][2]
  
# Print the variables
print("First Row Second Column =", first_row_second_column)
print("Second Row First Column =", second_row_first_column)
print("Third Row Third Column =", third_row_third_column)

输出:

示例 3:引用 3-D 数组中的项

Python数组示例

蟒蛇3

# Creating a 3d-array of elements
arr = [[[4, 6, 3], [2, 6, 8], [3, 5, 12]],
       [[32, 11, 4], [23, 53, 89], [19, 17, 10]],
       [[14, 22, 52], [56, 43, 99], [20, 37, 32]]]
  
# Referring elements of the 3d-array
# by 3d index to new variables
first_second_second = arr[0][1][1]
second_first_third = arr[1][0][2]
third_third_first = arr[2][2][0]
  
# Print the variables
print("First Second Second Value =", first_second_second)
print("Second First Third Value =", second_first_third)
print("Third Third First Value =", third_third_first)

输出:

Python 的 numpy 模块数组示例

蟒蛇3

# Importing numpy module
import numpy as np
  
# Creating a 3d-array of elements
arr = np.array([[[4, 6, 3], [2, 6, 8], [3, 5, 12]],
                [[32, 11, 4], [23, 53, 89], [19, 17, 10]],
                [[14, 22, 52], [56, 43, 99], [20, 37, 32]]])
  
# Referring elements of the 3d-array
# by 3d index to new variables
first_second_second = arr[0][1][1]
second_first_third = arr[1][0][2]
third_third_first = arr[2][2][0]
  
# Print the variables
print("First Second Second Value =", first_second_second)
print("Second First Third Value =", second_first_third)
print("Third Third First Value =", third_third_first)

输出 _

示例 4:引用数组的整行

Python数组示例

蟒蛇3

# Creating a 2d-array of elements
arr = [[4, 6, 3],
       [5, 9, 2],
       [1, 8, 7]]
  
# Referring rows of the 2d-array
# by row index to new variables
first_row = arr[0]
second_row = arr[1]
third_row = arr[2]
  
# Print the variables
print("First Row =", first_row)
print("Second Row =", second_row)
print("Third Row =", third_row)

输出:

Python 的 numpy 模块数组示例

蟒蛇3

# Importing numpy module
import numpy as np
  
# Creating a 2d-numpy-array of elements
arr = np.array([[4, 6, 3],
                [5, 9, 2],
                [1, 8, 7]])
  
# Referring rows of the 2d-array
# by row index to new variables
first_row = arr[0]
second_row = arr[1]
third_row = arr[2]
  
# Print the variables
print("First Row =", first_row)
print("Second Row =", second_row)
print("Third Row =", third_row)

输出: