python 中的矩阵

发布于 2024-09-04 14:32:14 字数 285 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

生活了然无味 2024-09-11 14:32:14

Python 不附带多维数组,但您可以通过流行的 numpy 第三方包添加它们。如果你想避免第三方包,你在Python中要做的就是使用列表的列表(每个“列表”是一个一维“类似向量”的序列,它可以保存任何类型的项目)。

例如:

data = [ [ [0 for i in range(4)] for j in range(5)] for k in range(6)]

这会创建一个包含 6 个项目的列表,其中包含 5 个项目的列表,这些项目是 4 个 0 的列表——即,一个 6 x 5 x 4“3D 矩阵”,然后您可以按照您想要的方式对其进行寻址,

for i in range(6):
  for j in range(5):
    data[i][j][0]=read(0)
    data[i][j][1]=read(1)
    data[i][j][2]=read(2)

以初始化第一个每个最嵌套的子列表中的四个项目中的三个都调用了那个神秘的函数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:

data = [ [ [0 for i in range(4)] for j in range(5)] for k in range(6)]

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,

for i in range(6):
  for j in range(5):
    data[i][j][0]=read(0)
    data[i][j][1]=read(1)
    data[i][j][2]=read(2)

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?).

旧时浪漫 2024-09-11 14:32:14

这取决于您的文件格式,但请查看:

链接

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

网名女生简单气质 2024-09-11 14:32:14

您可能想要使用 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

你的背包 2024-09-11 14:32:14

一个简单的例子是:

data = []
with open(_filename_, 'r') as f:
    for line in f:
        data.append([int(x) for x in line.split()])

A simple example would be:

data = []
with open(_filename_, 'r') as f:
    for line in f:
        data.append([int(x) for x in line.split()])
弃爱 2024-09-11 14:32:14

一种以矩阵形式扩展列表的方法。我已经浏览了 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.

So要识趣 2024-09-11 14:32:14

矩阵是二维结构。在普通 Python 中,矩阵最自然的表示形式是列表的列表。

因此,您可以将行矩阵写为:

[[1, 2, 3, 4]]

将列矩阵写为:

[[1],
 [2],
 [3],
 [4]]

这也可以很好地扩展到 mx n 矩阵:

[[10, 20],
 [30, 40],
 [50, 60]]

请参阅 matfunc.py 有关如何操作的示例用纯 Python 开发完整的矩阵包。
它的文档是 这里

这是一个使用列表列表表示在普通 python 中进行矩阵乘法的示例:

>>> from pprint import pprint
>>> def mmul(A, B):
        nr_a, nc_a = len(A), len(A[0])
        nr_b, nc_b = len(B), len(B[0])
        if nc_a != nr_b:
            raise ValueError('Mismatched rows and columns')
        return [[sum(A[i][k] * B[k][j] for k in range(nc_a))
                 for j in range(nc_b)] for i in range(nr_a)]

>>> A = [[1, 2, 3, 4]]
>>> B = [[1],
         [2],
         [3],
         [4]]

>>> pprint(mmul(A, B))
[[30]]

>>> pprint(mmul(B, A), width=20)
[[1, 2, 3, 4],
 [2, 4, 6, 8],
 [3, 6, 9, 12],
 [4, 8, 12, 16]]

正如另一位受访者提到的,如果您认真对待矩阵工作​​,那么您应该安装 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:

[[1, 2, 3, 4]]

And write a column matrix as:

[[1],
 [2],
 [3],
 [4]]

This extends nicely to m x n matrices as well:

[[10, 20],
 [30, 40],
 [50, 60]]

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:

>>> from pprint import pprint
>>> def mmul(A, B):
        nr_a, nc_a = len(A), len(A[0])
        nr_b, nc_b = len(B), len(B[0])
        if nc_a != nr_b:
            raise ValueError('Mismatched rows and columns')
        return [[sum(A[i][k] * B[k][j] for k in range(nc_a))
                 for j in range(nc_b)] for i in range(nr_a)]

>>> A = [[1, 2, 3, 4]]
>>> B = [[1],
         [2],
         [3],
         [4]]

>>> pprint(mmul(A, B))
[[30]]

>>> pprint(mmul(B, A), width=20)
[[1, 2, 3, 4],
 [2, 4, 6, 8],
 [3, 6, 9, 12],
 [4, 8, 12, 16]]

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文