By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

lists <-> tuple

P: n/a
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.
Nov 2 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Nov 2 '05 #2

P: n/a
Peter Notebaert wrote:
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
rk***@ucsd.edu

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

Nov 2 '05 #3

P: n/a

"Jim" <mr*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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!
Nov 5 '05 #4

P: n/a

"Robert Kern" <ro*********@gmail.com> wrote in message
news:ma*************************************@pytho n.org...
Peter Notebaert wrote:
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
rk***@ucsd.edu

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


Thanks!
Nov 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.