Re: [Tutor] Question on List Comprehensions
On 21-Nov-11 23:49, Charles Becker wrote: Alan, Steve, future readers, After some re-reading and hacking I was able to discover the solution. Since I raised the question here it is : [['{0}'.format(x+1), x+1] for x in range(size)] Just to fill out some other refinements for your information, if you're not planning to do anything special with the string formatting in each list, you don't really need to call format() when all it's doing is just making a string representation of the data value. so '{0}'.format(x+1) could just be str(x+1) Resulting in: [[str(x+1), x+1] for x in range(size)] Also, if you didn't like the repeated x+1 in there, you could just change the range call to go from 1..size directly: [[str(x), x] for x in range(1,size+1)] -- Steve Willoughby / st...@alchemy.com A ship in harbor is safe, but that is not what ships are built for. PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Question on List Comprehensions
I'm trying to use a list comprehension to build a list with a variable number of lists nested within it (ideally eventually going several levels of nesting). However I seem to be observing some strange behavior and was wondering if anyone could take a look at this and tell me if what I'm trying to do with list comps is possible, or is a map() or for loop the best thing here? I'm not worrying about incrementing the variables in the later examples since I'm confused about their behavior (just simply adding new elements to the list rather than nesting the lists; and then setting the list to [none] in the last uncommented. The last commented one produces a syntax error, is it impossible to be recursive with list comps like that or is my syntax just faulty?) Thanks! Charles Here's the raw code with my comments : board_size = 5 master_list = [] # this block produces the desired behavior for c in range(board_size): cell = ['', c+1] master_list.append(cell) print(master_list) # I don't understand why this block behaves the way it does master_list = [] master_list = [board_size * cell] print(master_list) # I also don't understand why this block behaves the way that it does master_list = [] master_list = [master_list.append(cell)] print(master_list) # this block produces a syntax error, and I'm not sure why ''' master_list = [] master_list = [x for c in range(board_size) master_list.append(cell)] print(master_list) ''' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question on List Comprehensions
Charles Karl Becker wrote: I'm trying to use a list comprehension to build a list with a variable number of lists nested within it (ideally eventually going several levels of nesting). However I seem to be observing some strange behavior and was wondering if anyone could take a look at this and tell me if what I'm trying to do with list comps is possible, or is a map() or for loop the best thing here? When in doubt, always use a for loop. List comps can't do anything that for loops can do, in fact they can do LESS. I'm not worrying about incrementing the variables in the later examples since I'm confused about their behavior (just simply adding new elements to the list rather than nesting the lists; and then setting the list to [none] in the last uncommented. The last commented one produces a syntax error, is it impossible to be recursive with list comps like that or is my syntax just faulty?) Your syntax is faulty. List comps are not full-blown replacements for for-loops, they are intentionally simple and straightforward. board_size = 5 master_list = [] # this block produces the desired behavior for c in range(board_size): cell = ['', c+1] master_list.append(cell) print(master_list) # I don't understand why this block behaves the way it does master_list = [] master_list = [board_size * cell] print(master_list) You start of with master_list set to an empty list. Then you immediately throw away that empty list, and set master_list to a list containing a single value, board_size * cell. Since by accident cell happens to equal a list left over from the previous part of code, you multiply a number 5 by a list ['', 5]. Multiplication of lists performs repetition: py ['a', 'b', 'c']*3 ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] By the way, there is no list comprehension there. All you have is a list containing a single item. [42] is not a list comp, it is a list containing a single item, 42. [4*x] is not a list comp, it is a list containing a single item, 4*x (whatever x happens to be). [4*x for x in (25, 36, 19, 5)] is a list comp. # I also don't understand why this block behaves the way that it does master_list = [] master_list = [master_list.append(cell)] print(master_list) You call master_list.append, which modifies master_list in place and returns None. So you append cell to master_list, then you create a new list [None], and set master_list to that new list, throwing away the work you did earlier. Again, there is no list comprehension here either. # this block produces a syntax error, and I'm not sure why ''' master_list = [] master_list = [x for c in range(board_size) master_list.append(cell)] print(master_list) ''' Because you don't have a list comprehension. You can't put add arbitrary code inside a square brackets [ ]. You have to follow the syntax for a list comprehension: listcomp = [expression for name in sequence] not listcomp = [expression for name in sequence another_command] -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question on List Comprehensions
On 22/11/11 00:10, Steven D'Aprano wrote: Because you don't have a list comprehension. You can't put add arbitrary code inside a square brackets [ ]. You have to follow the syntax for a list comprehension: listcomp = [expression for name in sequence] not listcomp = [expression for name in sequence another_command] And being picky you can add a conditional after the loop: listcomp = [expression for name in sequence if some_condition] But it must be an if test, nothing else will do. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question on List Comprehensions
Steven and Alan, Thank you for your comments! Alan said: Because you don't have a list comprehension. You can't put add arbitrary code inside a square brackets [ ]. You have to follow the syntax for a list comprehension: This helps me understand a lot when looking back, I thought that any operation done in place of defining the list literally was a list comprehension. I'm on wikipedia and a few tutorials now to refine and will post back when I've come up with the solution I'm looking for (and a comment as to if it's worth replacing a for loop with). Thanks again! Charles On Mon, Nov 21, 2011 at 7:04 PM, Alan Gauld alan.ga...@btinternet.com wrote: On 22/11/11 00:10, Steven D'Aprano wrote: Because you don't have a list comprehension. You can't put add arbitrary code inside a square brackets [ ]. You have to follow the syntax for a list comprehension: listcomp = [expression for name in sequence] not listcomp = [expression for name in sequence another_command] And being picky you can add a conditional after the loop: listcomp = [expression for name in sequence if some_condition] But it must be an if test, nothing else will do. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question on List Comprehensions
Alan, Steve, future readers, After some re-reading and hacking I was able to discover the solution. Since I raised the question here it is : [['{0}'.format(x+1), x+1] for x in range(size)] This will create the list with nested lists for whatever number 'size' is set to. This should be good enough to get anyone started on future problems in this area, and yes the redundancy present does make sense in the scope of what I'm working on. :) Thanks! I'll leave future questions on the backburner a little longer. Charles Sent from my iPhone On Nov 21, 2011, at 7:04 PM, Alan Gauld alan.ga...@btinternet.com wrote: On 22/11/11 00:10, Steven D'Aprano wrote: Because you don't have a list comprehension. You can't put add arbitrary code inside a square brackets [ ]. You have to follow the syntax for a list comprehension: listcomp = [expression for name in sequence] not listcomp = [expression for name in sequence another_command] And being picky you can add a conditional after the loop: listcomp = [expression for name in sequence if some_condition] But it must be an if test, nothing else will do. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor