RE: python and 2D lists

  • From: "Homme, James" <james.homme@xxxxxxxxxxxx>
  • To: "programmingblind@xxxxxxxxxxxxx" <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 2 Oct 2009 07:14:09 -0400

Hi Richard,
List comprehensions.

Thanks.

Jim

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Richard Dinger
Sent: Thursday, October 01, 2009 10:20 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: python and 2D lists

Hi Jim,

I would be happy to help, what part is not clear?  The discussion of list
comprehensions or the discussion of for loops or both?

Richard

----- Original Message -----
From: "Homme, James" <james.homme@xxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, October 01, 2009 5:16 AM
Subject: RE: python and 2D lists


Hi Richard,
I apologize. I find it very hard to think in code. Can you make this
explanation more english-like?

Thanks for your indulgence.

Jim

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Richard Dinger
Sent: Wednesday, September 30, 2009 8:26 PM
To: programmingblind@xxxxxxxxxxxxx
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


This e-mail and any attachments to it are confidential and are intended
solely for use of the individual or entity to whom they are addressed.  If
you have received this e-mail in error, please notify the sender immediately
and then delete it.  If you are not the intended recipient, you must not
keep, use, disclose, copy or distribute this e-mail without the author's
prior permission.  The views expressed in this e-mail message do not
necessarily represent the views of Highmark Inc., its subsidiaries, or
affiliates.
__________
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


This e-mail and any attachments to it are confidential and are intended solely 
for use of the individual or entity to whom they are addressed.  If you have 
received this e-mail in error, please notify the sender immediately and then 
delete it.  If you are not the intended recipient, you must not keep, use, 
disclose, copy or distribute this e-mail without the author's prior permission. 
 The views expressed in this e-mail message do not necessarily represent the 
views of Highmark Inc., its subsidiaries, or affiliates.
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: