Unit 5-5, row, column names swapped

0
1
newpath = [[0 for row in range(len(path[0]))] for col in range(len(path))]

OK, I'm all in favor of more descriptive variable names. But the variable row here runs along columns and the variable col runs along rows.

asked 22 Mar '12, 14:20

Anne%20Paulson's gravatar image

Anne Paulson
9.5k327698
accept rate: 39%

Didn't we have this discussion a while back? Oh wait... they haven't fixed it yet. Might just be sticking to their usual format.

(22 Mar '12, 14:26) yoshi yoshi's gravatar image

Yep, this mangled position of row and col have already been spotted a few days ago...

(22 Mar '12, 20:01) Jose Jose's gravatar image

7 Answers:

Or you could write even simpler (looks almost as pseudo-code now):

newpath = [[cell for cell in row] for row in path]
link

answered 22 Mar '12, 15:31

Gundega's gravatar image

Gundega ♦♦
44.0k70170315

I like how nice and clean this looks. Since we don't need the indices themselves, why bother with all than range(len(...)) stuff? That goes for initializing to 0 too:

newpath = [[0 for cell in row] for row in path]

(22 Mar '12, 19:26) yzzid yzzid's gravatar image

@yzzid yes, very true. It's so much more readable. There are cases when we need indices, but not in these initializations of lists

(22 Mar '12, 19:35) Gundega ♦♦ Gundega's gravatar image

one more

newpath = [row[:] for row in path]
link

answered 23 Mar '12, 05:26

Sergei%20Koryakin's gravatar image

Sergei Koryakin
6129

Good catch! Fixed in the programming quiz itself. :)

link

answered 22 Mar '12, 14:27

MichaelUdacity's gravatar image

MichaelUdacity ♦♦
2.9k62231

edited 22 Mar '12, 14:28

Yeah I thought about that too. Now I think of it like 'row' initializes a row and 'col' copies that row down to create the columns.

link

answered 22 Mar '12, 14:29

Troy%20Payne's gravatar image

Troy Payne
9691325

Another related point: you can reduce the deep copy to one line:

newpath = [[float(path[row][col]) for col in range(len(path[0]))] for row in range(len(path))]

link

answered 22 Mar '12, 15:05

limeyguru's gravatar image

limeyguru
1.3k71637

Interesting. I didn't put float on anything and it still all worked.

(22 Mar '12, 15:10) Anne Paulson Anne%20Paulson's gravatar image
from copy import deepcopy
newpath = deepcopy(path)

# but none of these will initialize newpath to all 0's though
link

answered 22 Mar '12, 15:48

Troy%20Payne's gravatar image

Troy Payne
9691325

edited 22 Mar '12, 15:49

In this case the whole point is making deep copy. Initializing to 0 (or any other number) is being done all the time with list comprehensions.

(22 Mar '12, 15:52) Gundega ♦♦ Gundega's gravatar image

These are all good suggestions, thanks

link

answered 23 Mar '12, 02:20

Sebastian%20Thrun's gravatar image

Sebastian Thrun
20.8k183754

Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×5,185
×2,216
×81

Asked: 22 Mar '12, 14:20

Seen: 453 times

Last updated: 23 Mar '12, 05:26