|
 |
|
 |
01-17-2007, 03:11 PM
|
#1 (permalink)
|
|
Code Monkey
Join Date: Mar 2005
Posts: 55
|
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.
|
|
|
01-18-2007, 11:29 AM
|
#2 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
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
|
|
|
02-05-2007, 11:19 AM
|
#3 (permalink)
|
|
Code Monkey
Join Date: Mar 2005
Posts: 55
|
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).
|
|
|
02-05-2007, 12:18 PM
|
#4 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
Sorry, maybe redhead will respond since he actually uses python.
__________________
Stop intellectual property from infringing on me
|
|
|
02-05-2007, 01:13 PM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
|
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
Last edited by redhead; 02-05-2007 at 01:43 PM.
|
|
|
02-05-2007, 02:53 PM
|
#6 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
Quote:
Originally Posted by redhead
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
|
|
|
02-05-2007, 03:27 PM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
|
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... 
Last edited by redhead; 02-05-2007 at 03:56 PM.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 10:40 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|