Python-字符串到矩阵表示 [英] Python - string to matrix representation

查看:878
本文介绍了Python-字符串到矩阵表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我有一个字符串a="1 2 3; 4 5 6".如何在Python中将其表示为矩阵[1 2 3; 4 5 6]?
  2. 然后我想使用另一个这样的字符串b,转换为矩阵并找到a x b.
  1. 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?
  2. I want to then use another such string b, convert to a matrix and find a 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆