python二维数组元素访问总结

python二维数组元素访问总结

给出列表

list = [[ 0.32332367, -0.09514636, -0.94149292],
        [-0.3874498,   0.89440238, -0.22344384],
        [ 0.86333334,  0.43702596,  0.25231701]]
list
# 正确访问二维数组中的元素
print(list[1][2])

# 输出:-0.22344384

# 错误访问二维数组中的元素
print(list[1,2])

# 输出:TypeError: list indices must be integers or slices, not tuple

array
import numpy as np

# 将list转换为array
array = np.array(list)

# 正确访问二维数组中的元素
print(array[1][2])

# 输出:-0.22344384

# 正确访问二维数组中的元素
print(list[1,2])

# 输出:-0.22344384

tuple

# 将list转换为tuple
tuple = tuple(list)

# 正确访问二维数组中的元素
print(tuple[1][2])

# 输出:-0.22344384

# 错误访问二维数组中的元素
print(tuple[1,2])

# 输出:TypeError: tuple indices must be integers or slices, not tuple

猜你喜欢

转载自blog.csdn.net/weixin_48319333/article/details/130029990