lists <-> tuple

  • Thread starter Peter Notebaert
  • Start date
P

Peter Notebaert

I am new to Python and have to create an import library in C that uses
matrices.

These matrices can be one-dimensional (vectors) or two-dimensional. If I
look in the ActivePython 2.4 documentation at data structures, then I see at
least 2 posibilities to represent them: Lists and Tuples.

The documention doesn't give me much information on what the best choice is
for the data type to provide/return these matrices.

So my question is, should I use lists or tuples to represent my matrices in
and why?

Thanks for any reaction.
 
J

Jim

Tuples or lists for matrix-like functionality?

Use lists. Tuples are meant for small immutable sets of things that go
together. Lists are more like arrays, and you can assign to one
existing element if you want.

One exception, is a short vector is often a tuple like (x, y, z) and
you might want to multiply that vector by your matrix. You can convert
a tuple to a list with list(aTuple) or back with tuple(aList.)

Even better, take a look at numarray (or numpy or scipy or scipy_core.)
They all have really nice matrix code and there are C APIs that let
you manipulate them. Chances are they do everything you're intending
to implement.

Immutability example:
tup = ("a", "b", "c")
tup[1] = "g"
Traceback (most recent call last):
File "<input>", line 1, in ?
TypeError: object does not support item assignment
lst = ["a", "b", "c"]
lst[1] = "g"
lst
['a', 'g', 'c']


-Jim
 
R

Robert Kern

Peter said:
I am new to Python and have to create an import library in C that uses
matrices.

These matrices can be one-dimensional (vectors) or two-dimensional. If I
look in the ActivePython 2.4 documentation at data structures, then I see at
least 2 posibilities to represent them: Lists and Tuples.

The documention doesn't give me much information on what the best choice is
for the data type to provide/return these matrices.

So my question is, should I use lists or tuples to represent my matrices in
and why?

You'll probably want to use scipy_core. It's a package designed
specifically to deal with multidimensional arrays of homogeneous,
(usually) numeric data.

http://numeric.scipy.org

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
P

Peter Notebaert

Jim said:
Tuples or lists for matrix-like functionality?

Use lists. Tuples are meant for small immutable sets of things that go
together. Lists are more like arrays, and you can assign to one
existing element if you want.

One exception, is a short vector is often a tuple like (x, y, z) and
you might want to multiply that vector by your matrix. You can convert
a tuple to a list with list(aTuple) or back with tuple(aList.)

Even better, take a look at numarray (or numpy or scipy or scipy_core.)
They all have really nice matrix code and there are C APIs that let
you manipulate them. Chances are they do everything you're intending
to implement.

Immutability example:
tup = ("a", "b", "c")
tup[1] = "g"
Traceback (most recent call last):
File "<input>", line 1, in ?
TypeError: object does not support item assignment
lst = ["a", "b", "c"]
lst[1] = "g"
lst
['a', 'g', 'c']


-Jim

Thanks!
 
P

Peter Notebaert

Robert Kern said:
You'll probably want to use scipy_core. It's a package designed
specifically to deal with multidimensional arrays of homogeneous,
(usually) numeric data.

http://numeric.scipy.org

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,270
Messages
2,571,339
Members
48,029
Latest member
Anchorman2022

Latest Threads

Top