代码如下:
import numpy as np
#---numpy.array方法
a = np.array( [ [10, -2, -1],
[-2, 10, -1],
[-1, -2, 5] ])
print (a[2][2])
print (a[2,2])
#---list方法
b = [ [10, -2, -1],
[-2, 10, -1],
[-1, -2, 5] ]
print (b[2][2])
print (b[2,2])
# 思考:程序结果如何?说明什么?
运行结果如下:
>>> %Run '思考_二维数组单个元素调用问题.py'
5
5
5
Traceback (most recent call last):
File "E:\YU304\njust304\LilunJiaoXue\HGJiSuan\0LECTURE_2XF\05线性方程组\Python\思考_二维数组单个元素调用问题.py", line 15, in <module>
print (b[2,2])
TypeError: list indices must be integers or slices, not tuple
>>>
最后一行运行错误,说明:
numpy.array方法和list方法均可用于生成二维数组;然而,numpy.array方法产生的二维数组元素调用可以使用a[2][2]或a[2,2],但是list方法只能使用a[2][2]。
联系客服