Re: python and 2D lists

  • From: "Alex Hall" <mehgcap@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 1 Oct 2009 07:20:36 -0400

I got it working (I think). At least the grid part works. The problem of errors is annoying; even in a shell, I see only my "start" command, followed by my home directory and a greater-than sign like usual, no errors at all.



Have a great day,
Alex
New email address: mehgcap@xxxxxxxxx
----- Original Message ----- From: "Richard Dinger" <rrdinger@xxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Wednesday, September 30, 2009 8:26 PM
Subject: Re: python and 2D lists


I think you are missing the errors because they just flash by on your screen without speaking. Try running in a command shell and you can see the errors.

I'm not sure exactly what you are trying to do, but what you have will not work. You need to study how loops work in Python. For example in your first loop:
for i in grid:

the first value of i is the first sublist of your list of lists e.g. "[(0,0), (0,1)}

In the next j for loop you try to use that list as an index into a two dimensional list. That makes no sense.

If you really want the value 4 in each location, do it in the list comprrehension:

grid = [[4 for c in ...] for r in ...]

----- Original Message ----- From: "Alex Hall" <mehgcap@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Wednesday, September 30, 2009 11:02 AM
Subject: Re: python and 2D lists


I tried it. Here is my entire, if very simple, program (this is not the one I am trying to get to work, this is just a proof-of-concept for a section of that one).

import win32com.client, time, sys
#speaker=win32com.client.Dispatch("Say.Tools")
grid=[[(r, c) for c in range(2)] for r in range(3)]
for i in grid:
for j in grid[i]:
 grid[i][j]=4
 #speaker.say(grid[i][j])
 print(grid[i][j])
# end for
# end for

When I run this, it does nothing; no errors, no printed fours, nothing. It has to be some simple error, but what?

Have a great day,
Alex
New email address: mehgcap@xxxxxxxxx
----- Original Message ----- From: "Richard Dinger" <rrdinger@xxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Wednesday, September 30, 2009 12:09 PM
Subject: Re: python and 2D lists


Hi,

Read up on list comprehensions. For example to create a 3 by 5 list of row and column index tuples:

l =[[(r, c) for c in range(5)] for r in range(3)]

The above can also be done with loops if you want.

You may also want to look at using a dictionary of dictionaries as an alternative approach to using lists.

Richard

----- Original Message ----- From: "Alex Hall" <mehgcap@xxxxxxxxx>
To: "Blind Programming List" <programmingblind@xxxxxxxxxxxxx>
Sent: Wednesday, September 30, 2009 7:39 AM
Subject: python and 2D lists


Hi all,

I have been trying for a few days now to get a 2d list, like a grid, working in python. It refuses to work, though, and nothing on Google explains what to do in a way that makes sense to someone as new to python as me. Below is, in Java, what I want to do in Python. Could someone please just translate the Java so I can figure it out? This is the main thing that is stopping a couple hundred lines of python code from running; once this is figured out, the rest should go fine.

Java:

String[] grid=new String[10][10];

//fill all elements with one message
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[i].length;j++){
grid[i][j]="Grid square "+i+", "+j+" now contains a string!";
System.out.println(grid[i][j]);
}
}

Thanks for any help!


Have a great day,
Alex
New email address: mehgcap@xxxxxxxxx
__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: