Python-字符串到矩阵表示 [英] Python - string to matrix representation
本文介绍了Python-字符串到矩阵表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 我有一个字符串
a="1 2 3; 4 5 6"
.如何在Python中将其表示为矩阵[1 2 3; 4 5 6]
? - 然后我想使用另一个这样的字符串
b
,转换为矩阵并找到a x b
.
- I have a string
a="1 2 3; 4 5 6"
. How do i express this as a matrix[1 2 3; 4 5 6]
in Python? - I want to then use another such string
b
, convert to a matrix and finda x b
.
推荐答案
您可以使用 numpy
模块可直接从Matlab类型格式的字符串中创建矩阵
You can use the numpy
module to create a matrix directly from a string in matlab type format
>>> import numpy as np
>>> a="1 2 3; 4 5 6"
>>> np.matrix(a)
matrix([[1, 2, 3],
[4, 5, 6]])
您可以使用相同的库进行矩阵乘法
You can use the same library to do matrix multiplication
>>> A = np.matrix("1 2 3; 4 5 6")
>>> B = np.matrix("2 3; 4 5; 6 7")
>>> A * B
matrix([[28, 34],
[64, 79]])
在numpy
库上进行阅读,这是一个非常强大的模块,可以完成您所引用的所有类型的工作.
Go read up on the numpy
library, it is a very powerful module to do all of the type of work that you are referring to.
这篇关于Python-字符串到矩阵表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文