Arrays? (Or lists if you prefer)

H

Hakusa

Pythonic lists are everything I want in a one dimensional array . . .
but I'm trying to do a text adventure and simplify the proces by
creating a grid as opposed to a tedious list of rooms this room
connects to.

So I want to know a good way to do a SIMPLE two dimensional array.
Python's lists are a little confusing when it comes to how to do this.
I realized that I could do

list = [ [0] * N ] * N

but I don't know if it would work because I'm a newbie and only
understand arrays if they're in grid-like form. And I haven't ben thru
linear algebra yet, due to my school giving me a few set backs I'm
allready having to take geometry and allgebra II which are meant to be
taken one after another at my school, so any suggestions or hints in
that direction won't be helpful.

So:
Way to do SIMPLE array, either internally or externally, with Python,
please.
 
N

Neil Cerutti

Pythonic lists are everything I want in a one dimensional array
. . . but I'm trying to do a text adventure and simplify the
proces by creating a grid as opposed to a tedious list of rooms
this room connects to.

Not to chase you out of comp.lang.python, but take a stroll by
rec.arts.int-fiction for pointers to a selection of domain
specific programming languages and virtual machines for writing
text adventures.
So I want to know a good way to do a SIMPLE two dimensional
array. Python's lists are a little confusing when it comes to
how to do this. I realized that I could do

list = [ [0] * N ] * N

You're right to be suspicious of that construct.
>>> a = [[0] * 2] * 2
>>> a [[0, 0], [0, 0]]
>>> a[0][1] = "Oops."
>>> a
[[0, 'Oops.'], [0, 'Oops.']]

The problem is that Python lists hold references, not objects,
combined with the behavior of the multiplication operator on
lists.
but I don't know if it would work because I'm a newbie and only
understand arrays if they're in grid-like form. And I haven't
ben thru linear algebra yet, due to my school giving me a few
set backs I'm allready having to take geometry and allgebra II
which are meant to be taken one after another at my school, so
any suggestions or hints in that direction won't be helpful.

So:
Way to do SIMPLE array, either internally or externally, with
Python, please.

The problem wasn't with the construct, but in the way it was
constructed. Just build it up bit by bit, or build it all at once
using range() and then fill it in afterwards.
>>> b =[range(2), range(2)]
>>> b [0, 1], [0, 1]]
>>> b[0][1] = "OK."
>>> b
[0, 'OK.'], [0, 1]]

A flexible way to do it instead might be to make your data
attributes of objects, instead. It won't be as fast as decoding
multi-dimensional arrays, but it'll be easier to work with.

Then again, there's Inform, TADS, HUGO, the Z-machine, Glk and
Adrift to consider. ;-)
 
P

Paul McGuire

Pythonic lists are everything I want in a one dimensional array . . .
but I'm trying to do a text adventure and simplify the proces by
creating a grid as opposed to a tedious list of rooms this room
connects to.
Way to do SIMPLE array, either internally or externally, with Python,
please.

As an example of using pyparsing, I chose a simple text adventure
application, and had to create a 2-D grid of rooms. The presentation
materials are online at http://www.python.org/pycon/2006/papers/4/, and the
source code is included with the examples that ship with pyparsing
 
H

Hakusa

Niel said:
Just build it up bit by bit, or build it all at once
using range() and then fill it in afterwards.
b =[range(2), range(2)]
b [0, 1], [0, 1]]
b[0][1] = "OK."
b
[0, 'OK.'], [0, 1]]

Interesting. Could I do . . . let's say

b = [range(range(3)]

for a three-dimensional array?


Paul said:
As an example of using pyparsing, I chose a simple text adventure
application, and had to create a 2-D grid of rooms. The presentation
materials are online at http://www.python.org/pycon/2006/papers/4/, and the
source code is included with the examples that ship with pyparsing

I read your entire thing, but how much stuck is a testimate to how much
I have yet to learn. Also, I didn't see the source code or downoad or
anything there. Where is it again?

Thanks for the responces.
 
H

Hendrik van Rooyen

So:
Way to do SIMPLE array, either internally or externally, with Python,
please.

to help you see it - here is a simple 3 row by 3 column list:

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

the first "row" is myarray[0] - ie the list [1,2,3]

the middle element is myarray[1][1] - ie 5 - row 1, col 1.

the last element in the first row is myarray[0][2] - ie 3

play with it at the interactive prompt...

HTH - Hendrik
 
H

Hakusa

Andrea said:
Neil said:
b =[range(2), range(2)]

I often happened to use

b = [[0] * N for i in xrange(N)]

an approach that can also scale up in dimensions;
for example for a cubic NxNxN matrix:

b = [[[0] * N for i in xrange(N)]
for j in xrange(N)]

Andrea

What's the difference between xrange and range?
 
F

Fredrik Lundh

What's the difference between xrange and range?

range() creates a list object and fills it in up front, xrange() returns
a sequence-like object that generates indexes on demand.

for short loops, this difference doesn't really matter. for large
loops, or if you usually don't run the loop until the end, xrange()
can be more efficient. xrange() also lets you do things like:

for x in xrange(sys.maxint):
...
if some condition:
break

without running out of memory.

</F>
 
F

Fredrik Lundh

Interesting. Could I do . . . let's say

b = [range(range(3)]

for a three-dimensional array?
>>> [range(range(3))]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: range() integer end argument expected, got list.

if your mail program is easier to reach than your Python interpreter
window, something's wrong with your setup.

</F>
 
H

Hakusa

Fredrik said:
Interesting. Could I do . . . let's say

b = [range(range(3)]

for a three-dimensional array?
[range(range(3))]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: range() integer end argument expected, got list.

if your mail program is easier to reach than your Python interpreter
window, something's wrong with your setup.

</F>

lol. Sad lol.
My mail button is actually easier to reach than my Python Interpreter,
especially when I wrote that quickly befoer going to bed . . . although
other than that, I just find it hard to program recently because school
has been taxing most of my intellectual anything, but yestarday I
thought I'd get back into the string. So nothing is wrong with my set
up, just my over-stressed brain.
 
P

Paul McGuire

I read your entire thing, but how much stuck is a testimate to how much
I have yet to learn. Also, I didn't see the source code or downoad or
anything there. Where is it again?

Thanks for the responces.

Download pyparsing at http://sourceforge.net/projects/pyparsing . Be sure
to download either the source installation or the doc installation - the
windows binary install omits the doc and example files.

The pyparsing home page is at http://pyparsing.wikispaces.com.

-- Paul
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top