473,789 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
that direction won't be helpful.

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

Oct 23 '06 #1
11 1526
"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
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
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,
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
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,
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

Oct 23 '06 #10

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

Similar topics

21
4334
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 in l] but I was hoping for something more like l. Hilde
11
2321
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 from -3 to 6. I'm completely lost on how I should create that array. Any shoves in the right direction would be appreciated. void test2() { system("cls"); cout << "2. Array declared with two integers: IntArray b(-3, 6);" <<
11
2288
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
1
1231
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) myArrayList.add(myobject) Question: what is the difference if you handle myobject form myArrayList like this :
38
10179
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. E.g. these two arrays would test as equal: servers = "Admin" servers = "Finance" servers = "Payroll" servers = "Sales"
14
12199
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++ books (Effective C++, More Effective C++, Exceptional C++, More Exceptional C++, C++ FAQs, Addison Wesley 2nd Edition) Authors of these books have not considered UNICODE. So many of their
51
8654
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 for linked lists, I couldn't find one. I read something about Mcilroy's "Optimistic Merge Sort" and studied some implementation, but they were for arrays. Does anybody know if Mcilroys optimization is applicable to truly linked lists at all?
12
4169
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 like this: ('eco', "(u'Roads',)", 0.073969887301348305) Pysqlite doesn't like the format of the middle term: pysqlite2.dbapi2.InterfaceError: Error binding parameter 1 - probably
5
2193
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: ################ one.py ############## import array a = array.array('i')
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.