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. 11 1500
"Ha****@gmail.com" <Ha****@gmail.comwrites:
Way to do SIMPLE array, either internally or externally, with Python,
Maybe you want to use a dictionary:
a = {}
a[(3,5)] = 2
On 2006-10-23, Ha****@gmail.com <Ha****@gmail.comwrote:
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. ;-)
--
Neil Cerutti
<Ha****@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.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,
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
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
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.
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? Ha****@gmail.com 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.maxint):
...
if some condition:
break
without running out of memory.
</F> Ha****@gmail.com 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>
From: <Ha****@gmail.comwrote:
8<---------------------------------------
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
Fredrik Lundh wrote:
Ha****@gmail.com 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>
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.
<Ha****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Hilde Roth |
last post by:
This may have been asked before but I can't find it. If I have
a rectangular list of lists, say, l = ,,], is
there a handy syntax for retrieving the ith item of every sublist?
I know about for i...
|
by: - Steve - |
last post by:
For a school assignment I need to write a class to work with the
following code. The IntArray b(-3, 6) basically means that I need to
produce an array of integer values that has an index going...
|
by: Soeren Sonnenburg |
last post by:
Hi all,
Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:
a=
>>> b=
>>> a+b
|
by: jg |
last post by:
Im not a beginner in vb.net, but I never asked myself what is the difference
between folowing lines of code:
1.)You add a object to an array/arraylist/collection (lets take arraylists)
...
|
by: ssg31415926 |
last post by:
I need to compare two string arrays defined as string such that the
two arrays are equal if the contents of the two are the same, where
order doesn't matter and every element must be unique.
...
|
by: rohitpatel9999 |
last post by:
Hi
While developing any software, developer need to think about it's
possible enhancement for international usage and considering UNICODE.
I have read many nice articles/items in advanced C++...
|
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort...
|
by: rshepard |
last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding
the solution in the docs I have here.
Data are assembled for writing to a database table. A representative tuple
looks...
|
by: Tobiah |
last post by:
I checked out the array module today. It claims that
arrays are 'efficient'. I figured that this must mean
that they are faster than lists, but this doesn't seem
to be the case:
...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |