473,836 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2291
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*******@YOUR thirdtower.com. imaginationwrot e:
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 "multidimension al 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
2660
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 same speed? Or, would the array process faster? I'm new to Python, so my question may expose my ignorance. I appreciate anyone's effort to help me understand. Thanks. It is much appreciated.
8
3870
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 = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE, char*buf); Users will get a Numeric Array object and can change its values (and actually change the underlying C array).
2
4460
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 */ #include <time.h>
15
2358
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 computers memory? Or does Python hide such implementation details that deep, that there is no way to get down to them? The test code below shows, that extracting bits from an integer value n is faster when using n&0x01 than when using n%2 and I...
1
1743
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.
0
10862
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10262
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6986
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5838
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4468
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3119
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.