473,382 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

python list/array question...

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][i] = g[i]
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

Jul 5 '06 #1
5 2254
bruce wrote:
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][i] = g[i]
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 ?

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 5 '06 #2
Sybren Stuvel wrote:
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.
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
Jul 5 '06 #3
Sybren Stuvel <sy*******@YOURthirdtower.com.imaginationwrote:
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)
<type 'array'>
>>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
Jul 5 '06 #4
Sybren Stuvel wrote:
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.
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 ?-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 5 '06 #5
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][i] way, and it's easily
extensible over more than two dimensions ([k][j][i]??? 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

Jul 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: 2mc | last post by:
Generally speaking, if one had a list (from regular Python) and an array (from Numerical Python) that contained the same number of elements, would a While loop or a For loop process them at the...
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
15
by: Claudio Grondi | last post by:
Let's consider a test source code given at the very end of this posting. The question is if Python allows somehow access to the bytes of the representation of a long integer or integer in...
1
by: bruce | last post by:
Hi Paul... Thanks for the reply. Came to the same conclusion a few minutes before I saw your email. Another question: tr=d.xpath(foo) gets me an array of nodes.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.