473,851 Members | 2,185 Online

# Arrays? (Or lists if you prefer)

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

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

Oct 23 '06 #1
11 1532
"Ha****@gmail.c om" <Ha****@gmail.c omwrites:
Way to do SIMPLE array, either internally or externally, with Python,
Maybe you want to use a dictionary:

a = {}
a[(3,5)] = 2
Oct 23 '06 #2
On 2006-10-23, Ha****@gmail.co m <Ha****@gmail.c omwrote:
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.pytho n, but take a stroll by
rec.arts.int-fiction for pointers to a selection of domain
specific programming languages and virtual machines for writing
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
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

--
Neil Cerutti
Oct 23 '06 #3
<Ha****@gmail.c omwrote in message
news:11******** **************@ f16g2000cwb.goo glegroups.com.. .
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.
<snip>
Way to do SIMPLE array, either internally or externally, with Python,
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
Oct 23 '06 #4
Neil Cerutti wrote:
>>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
Oct 23 '06 #5
Niel Cerutti wrote:
>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 McGuire wrote:
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.

Oct 23 '06 #6

Andrea Griffini wrote:
Neil Cerutti wrote:
>>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?

Oct 23 '06 #7
Ha****@gmail.co m wrote:
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.maxi nt):
...
if some condition:
break

without running out of memory.

</F>

Oct 23 '06 #8
Ha****@gmail.co m wrote:
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>

Oct 23 '06 #9
From: <Ha****@gmail.c omwrote:
8<---------------------------------------
So:
Way to do SIMPLE array, either internally or externally, with Python,
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

Oct 23 '06 #10

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