python list/array question...

B

bruce

hi...

i'm trying to deal with multi-dimension lists/arrays

i'd like to define a multi-dimension string list, and then manipulate the
list as i need... primarily to add lists/information to the 'list/array' and
to compare the existing list information to new lists

i'm not sure if i need to import modules, or if the base python install i
have is sufficient. an example, or pointer to examples would be good...

i'd like

define a[][]

#basically, i'd like a 3x3 array, where each element
#has one of the a,b,c items..
# |a1, b1, c1|
# |a2, b2, c2|
# |a3, b3, c3|

a[1][1] = ['a1','b1','c1']
a[1][2] = ['a2','b2','c2']
a[1][3] = ['a3','b3','c3']

b = ['f','g','h']
v = ['f1','g1','h1']

if a[1][2] == b
print 'good!'

a[1][4] = b

x = 4
g = ['p1','l1','g1']

for i in range[g]
a[x] = g


these are the kinds of list/array functions i'd like to be able to
accomplish

pointers/code samples/pointers to code would be helpful...

and yeah. i've been looking via google...

thanks

-bruce
 
B

Bruno Desthuilliers

bruce said:
hi...

i'm trying to deal with multi-dimension lists/arrays

Python has lists (which AFAIK really are arrays not linked lists, but
they are called 'lists'). FWIW, this is in the fine manual.
i'd like to define a multi-dimension string list, and then manipulate the
list as i need... primarily to add lists/information to the 'list/array' and
to compare the existing list information to new lists

i'm not sure if i need to import modules, or if the base python install i
have is sufficient.

?????

importing modules doesn't require installing additional packages (unless
the modules you want to import are not part of the stdlib nor of your
application).
an example, or pointer to examples would be good...
http://www.python.org/doc/

i'd like

define a[][]

No "define" statement in Python - as you would know if you had read the
fine manual.
#basically, i'd like a 3x3 array, where each element
#has one of the a,b,c items..
# |a1, b1, c1|
# |a2, b2, c2|
# |a3, b3, c3|

a[1][1] = ['a1','b1','c1']

Python's list are zero-based (which is the common case). This is
mentionned in the fine manual.
a[1][2] = ['a2','b2','c2']
a[1][3] = ['a3','b3','c3']

a = [
['a1','b1','c1'],
['a2','b2','c2'],
['a3','b3','c3'],
]

or

a = []
a.append(['a1','b1','c1'])
a.append(['a2','b2','c2'])
a.append(['a3','b3','c3'])

(etc - cf the fine manual).
b = ['f','g','h']
v = ['f1','g1','h1']

if a[1][2] == b
print 'good!'

a[1][4] = b

x = 4
g = ['p1','l1','g1']

for i in range[g]
a[x] = g


these are the kinds of list/array functions i'd like to be able to
accomplish

pointers/code samples/pointers to code would be helpful...


start here : http://www.python.org/doc/
and yeah. i've been looking via google...

Really ?
 
S

Sybren Stuvel

Bruno Desthuilliers enlightened us with:
Python has lists (which AFAIK really are arrays not linked lists,
but they are called 'lists').

An array is generally understood as a list of items of the same type,
hence Python lists aren't arrays.

Sybren
 
D

Diez B. Roggisch

Sybren said:
Bruno Desthuilliers enlightened us with:

An array is generally understood as a list of items of the same type,
hence Python lists aren't arrays.

Only in the same sense as lists are. Inhomogeneous lists are of course
possible in python, and maybe even common, but many people argue that is
bad style. And in FP languages they are especially frown upon.

And AFAIK the internal representation is an array of object-pointers.

Regards,

Diez
 
A

Alex Martelli

Sybren Stuvel said:
Bruno Desthuilliers enlightened us with:
Python has lists (which AFAIK really are arrays not linked lists,
but they are called 'lists').

An array is generally understood as a list of items of the same type,
hence Python lists aren't arrays.
Hmmm...
x = Numeric.array([23, 4.5, 'zap!'], 'O')
type(x)
array([23 , 4.5 , zap! ],'O')

Should I think that an array (Numeric.array) is not an array? After all
it can hold just the same variety of item types as a Python list. Or is
it more useful to say that the "type" (which all the items have in
common) is "Python object"? (After all, 'object' IS what the typecode
letter 'O' stands for). I definitely like to think of Numeric.array's
as arrays, and I think it's both proper and useful to do so...


Alex
 
B

Bruno Desthuilliers

Sybren said:
Bruno Desthuilliers enlightened us with:



An array is generally understood as a list of items of the same type,
hence Python lists aren't arrays.

A list is generally understood as a linked list, hence Python lists are
not lists !-)

Also, in statically typed languages, lists are supposed to be homogenous
ordered collections of variable length (cf ML/Haskell...). And FWIW,
this is also the intended semantic for Python lists (cf GvR's comments
on list vs tuple respective semantics)

But, to be honnest:
in a lot of languages, an array is supposed to be of fixed size, hence
Python lists aren't arrays !-)

Now the question is: since Python lists are neither arrays nor lists,
how should we name them ?-)
 
D

Dennis Lee Bieber

i'm trying to deal with multi-dimension lists/arrays

i'd like to define a multi-dimension string list, and then manipulate the
list as i need... primarily to add lists/information to the 'list/array' and
to compare the existing list information to new lists
And google is down?
http://www.google.com/search?hl=en&...t&cd=1&q=python+list+multidimensional&spell=1

Second entry:
http://pyfaq.infogami.com/how-do-i-create-a-multidimensional-list

and yeah. i've been looking via google...
And didn't you find the above?
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
V

Vic.Kelson

Another way to do it is using a dict with keys that are tuples:
arr = {}
arr[0,0] = 'a1'
arr[0,1] = 'a2'
arr[1,0] = 'b1'
arr[1,1] = 'b2'
arr[2,0] = 'c1'
arr[2,1] = 'c2'
for j in range(3):
.... for i in range(2):
.... print arr[j,i], ' ',
.... print
....
a1 a2
b1 b2
c1 c2
You can derive a class from dict that implements desired behaviors
(e.g. bounds checking, nrows and ncols attributes). Using this approach
over lists of lists is nice: old fogey scientific programmers like me
prefer the explicit [j,i] syntax over the [j] way, and it's easily
extensible over more than two dimensions ([k][j]??? Yikes!). This is
perhaps not preferred for a "full" matrix (I'd tend to use a NumPy
array of Objects), but it can be useful.

I've used it in two cases:

-- A "sparse" matrix (overload dict.__getitem__ to return 0 if the
tuple (j,i) is not a valid key),

-- A "multidimensional array" in which all the indices are strings,
like a table with column labels and row labels (in my case, it was a
3-dimensional "table" with a concatenated key of three text strings but
there were reasons not to use a relational database). It was very
convenient to implement the code in this way, and extremely readable.

This trick is also useful with dbm files, e.g. using shelve.

--vic
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top