python 中的矩阵
我对 Python 非常陌生,我需要从文件中读取数字并将它们存储在矩阵中,就像我在 Fortran 或 C 中所做的那样;
for i
for j
data[i][j][0]=read(0)
data[i][j][1]=read(1)
data[i][j][2]=read(2)
...
...
我怎样才能在Python中做同样的事情?我读了一点,但对元组和类似的东西感到困惑
如果你能给我指出一个类似的例子,那就太好了,
谢谢
I am very new to Python, I need to read numbers from a file and store them in a matrix like I would do it in fortran or C;
for i
for j
data[i][j][0]=read(0)
data[i][j][1]=read(1)
data[i][j][2]=read(2)
...
...
How can I do the same in Python? I read a bit but got confused with tuples and similar things
If you could point me to a similar example it would be great
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Python 不附带多维数组,但您可以通过流行的 numpy 第三方包添加它们。如果你想避免第三方包,你在Python中要做的就是使用列表的列表(每个“列表”是一个一维“类似向量”的序列,它可以保存任何类型的项目)。
例如:
这会创建一个包含 6 个项目的列表,其中包含 5 个项目的列表,这些项目是 4 个 0 的列表——即,一个 6 x 5 x 4“3D 矩阵”,然后您可以按照您想要的方式对其进行寻址,
以初始化第一个每个最嵌套的子列表中的四个项目中的三个都调用了那个神秘的函数
read
,大概你想自己编写这个函数(我不知道它应该做什么——而不是“读取并返回下一个数字”,因为它需要一个神秘的论证,但是,然后呢?)。Python doesn't come with multi-dimensional arrays, though you can add them through the popular numpy third-party package. If you want to avoid third-party packages, what you would do in Python would be to use a list of lists of lists (each "list" being a 1-D "vector-like" sequence, which can hold items of any type).
For example:
this makes a list of 6 items which are lists of 5 items which are lists of 4 0's -- i.e., a 6 x 5 x 4 "3D matrix" which you could then address the way you want,
to initialize the first three of the four items on each most-nested sublist with calls to that mysterious function
read
which presumably you want to write yourself (I have no idea what it's supposed to do -- not "read and return the next number" since it takes a mysterious argument, but, then what?).这取决于您的文件格式,但请查看:
链接
和
http://docs.scipy.org/doc/scipy/reference/教程/io.html
It depends on your file format, but take a look on:
Link
and
http://docs.scipy.org/doc/scipy/reference/tutorial/io.html
您可能想要使用 numpy 并使用内置函数来使用 I/O,特别是 loadtxt。
http://docs.scipy.org/doc/numpy/参考/生成/numpy.loadtxt.html
有很多令人上瘾的函数来处理 I/O:
http://docs.scipy.org/doc/numpy/reference/routines.io.html
You may want to use numpy and use the built in function for using I/O, in particular loadtxt.
http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html
There are a lot of addictional functions to handle I/O:
http://docs.scipy.org/doc/numpy/reference/routines.io.html
一个简单的例子是:
A simple example would be:
一种以矩阵形式扩展列表的方法。我已经浏览了 python 中的其他矩阵代码,所有代码都使用推导式首先初始化所需大小的列表,然后更新值(这需要更多时间)。
让 r 代表行,c 代表列。
r = input('输入行大小:')
c = input('请输入列大小:')
米 = []
对于范围 (r) 内的 i:
m.追加([])
对于范围 (c) 中的 j:
m[i].append(input())
对于 m 中的 i:
print i
在这里,您可以输入矩阵的元素,就像在“C”或等效语言中一样。希望这可以帮助人们对实现矩阵有不同的看法。
A way to extend the list in a form to work like matrix. I have gone through other codes of matrix in python, all are using comprehensions to first initialize a list of required size and then update the values (which takes a little more time).
Let the r represents row and c for column.
r = input('Enter row size: ')
c = input('Enter column size: ')
m = []
for i in range(r):
m.append([])
for j in range(c):
m[i].append(input())
for i in m:
print i
Here, you can input the elements of matrix as it was in 'C' or equivalent languages. Hope this may help someone a different view of implementing matrices.
矩阵是二维结构。在普通 Python 中,矩阵最自然的表示形式是列表的列表。
因此,您可以将行矩阵写为:
将列矩阵写为:
这也可以很好地扩展到 mx n 矩阵:
请参阅 matfunc.py 有关如何操作的示例用纯 Python 开发完整的矩阵包。
它的文档是 这里。
这是一个使用列表列表表示在普通 python 中进行矩阵乘法的示例:
正如另一位受访者提到的,如果您认真对待矩阵工作,那么您应该安装 numpy 它直接支持许多矩阵运算:
Matrices are two dimensional structures. In plain Python, the most natural representation of a matrix is as a list of lists.
So, you can write a row matrix as:
And write a column matrix as:
This extends nicely to m x n matrices as well:
See matfunc.py for an example of how to develop a full matrix package in pure Python.
The documentation for it is here.
And here is a worked-out example of doing matrix multiplication in plain python using a list-of-lists representation:
As another respondent mentioned, if you get serious about doing matrix work, it would behoove you to install numpy which has direct support for many matrix operations: