Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-17-2007, 04:11 PM   #1 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 56
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
Python - Multidimensional arrays?

I just figured out how to do arrays in python.
I have a C++ program that spits out a binary float array
float f[8][5][60];

In python script that reads it in, I do this right now:

Code:
f = file("d:/float.bin", "rb")
a = array('f')
a.fromfile(f, 8*5*60)
And I do the indexing math myself to get an element.

What is the preferred method for using multidimensional arrays in python?

THanks.
QUantumAnenome is offline   Reply With Quote
Old 01-18-2007, 12:29 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
I think you can just naturally nest lists
Code:
Python 2.4 (#1, Mar 22 2005, 21:42:42)
>>> a = [1,[2,3],4]
>>> a
[1, [2, 3], 4]
>>> a[2]
4
>>> a[1][1]
3
>>> a.append(5)
>>> a[3]
5
>>> a
[1, [2, 3], 4, 5]
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 02-05-2007, 12:19 PM   #3 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 56
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
Thanks, but I don't see how that is better. The code to read in the file and insert floats into 3 deep nested lists would be much more complex, and would it also be slower than using a 1D array and indexing into it myself to fake a 3D array? The benefit looks like a syntaticl one, in that I can write a[x][y][z] instead of a(x, y, z).
QUantumAnenome is offline   Reply With Quote
Old 02-05-2007, 01:18 PM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Sorry, maybe redhead will respond since he actually uses python.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 02-05-2007, 02:13 PM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Quote:
Originally Posted by teknomage1
maybe redhead will respond since he actually uses python
Sorry tekno, I dont use python at a regular basis, I know of it and how it should be used, but I'm not currently involved in projects developed in the language. Thats why I havn't listed it as one of my known programming languages.

QUantumAnenome, In a case like the one you're looking for, you'd use the Comma Separated Values module. Simply have your file comma seperated, or tab or whatever you like, and use the module to convert it back to your orriginal list.

Say you're using Excel as the delimiter you can store and retrieve your data in a matter like this:
Code:
import csv

a=[1,2,3,[4,5],6,[7,8,[9]],0]

writer = csv.writer(open("my_file", "wb"), csv.excel)
writer.writerows(a)
writer.close()

b=array()
reader = csv.reader(open("my_file", "rb"), csv.excel)
try:
    for row in reader:
        b.append(row)
except csv.Error, e:
    sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
reader.close()

print a
print b
Or perhaps you just want the easy and quick answer to this one... By using the pickle serialization
Code:
import pickle
a = [1,2,3,[4,5,6],7,[8,[9]],0]
output = open('my_file', 'wb')
pickle.dump(a, output)
output.close()

input = open('my_file', 'rb')
b = pickle.load(input)
input.close()

print a
print b

a == b
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 02-05-2007 at 02:43 PM.
redhead is offline   Reply With Quote
Old 02-05-2007, 03:53 PM   #6 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Quote:
Originally Posted by redhead View Post
Sorry tekno, I dont use python at a regular basis, I know of it and how it should be used, but I'm not currently involved in projects developed in the language. Thats why I haven't listed it as one of my known programming languages.
D'oh! Sorry to put you on the spot like that, I just searched the forum for the last couple answers to python questions.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 02-05-2007, 04:27 PM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Dont feel bad, I like a challenge once in a while.. And it's not like Python has the most complex syntax tree to get involved with.

I would much rather be forced to explain something in python than in forinstance the ZT language or BrainFsck language or perhaps the Whitespace language

Now that would be a challenge in itself...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 02-05-2007 at 04:56 PM.
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
web log in using python prashmohan All Other Coding Languages 2 12-16-2005 01:46 AM
Do Multidimensional arrays work like a hash in Perl? philthee Java 1 10-22-2004 02:06 PM
New Tutorial: Python Lists Overview sde Code Newbie News 3 03-24-2004 10:54 AM
PyCon 2004 - Python Developers Conference sde Code Newbie News 0 02-12-2004 04:45 PM


All times are GMT -8. The time now is 12:58 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting