为了账号安全,请及时绑定邮箱和手机立即绑定

Python, Matrix of matrixs

Python, Matrix of matrixs

小唯快跑啊 2022-08-25 14:35:06
我需要创建一个矩阵,其元素包含矩阵,内部矩阵包含数字。如何访问内部矩阵,以及如何访问包含这些矩阵的数字。ext_matrix = []int_matrixes = [[1,2,3],[1,2,3]]ext_matrix.append(int_matrixes)我尝试了这个解决方案,但如果我需要一个外部2x2矩阵或内部2x2,它不起作用
查看完整描述

1 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

可以使用索引来获取项目。


ext_matrix = [] 

int_matrixes = [[0,1,2],[10,11,12],[20,21,22]]

ext_matrix.append(int_matrixes)


ext_matrix2 = []

ext_matrix2.extend(int_matrixes)


## [[[0, 1, 2], [10, 11, 12], [20, 21, 22]]]

print(ext_matrix)


## [20, 21, 22]

print(ext_matrix[0][2])


## 21

print(ext_matrix[0][2][1])


## [[0, 1, 2], [10, 11, 12], [20, 21, 22]]

print(ext_matrix2)


## [20, 21, 22]

print(ext_matrix2[2])


## 21

print(ext_matrix2[2][1])

或者你可以从numpy使用ndarray:


import numpy as np


ext_matrix = np.array([[ 0, 1, 2],

                       [10, 11, 12],

                       [20, 21, 22]])


## [[ 0  1  2]

##  [10 11 12]

##  [20 21 22]]

print(ext_matrix)


## [20 21 22]

print(ext_matrix[2])


## 21

print(ext_matrix[2][1])


查看完整回答
反对 回复 2022-08-25
  • 1 回答
  • 0 关注
  • 67 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号