开发者

Matrix in 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


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


It depends on your file format, but take a look on:

Link and http://docs.scipy.org/doc/scipy/reference/tutorial/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:

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


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.


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:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜