Re: Recursive list comprehension

2004-12-08 Thread Nick Craig-Wood
Peter Otten <[EMAIL PROTECTED]> wrote: > Adam DePrince wrote: > > > def flatten( i ): > > try: > > i = i.__iter__() > > while 1: > > j = flatten( i.next() ) > > try: > > while 1: > > yield j.next() > > exc

Re: Recursive list comprehension

2004-12-08 Thread Steven Bethard
Terry Reedy wrote: This is a ways off, if ever, but I think the general advice for user code is to use the newer protocol. Yes, definitely. I hope no one misconstrued me to be suggesting that you should use the 'sequence protocol' for iterators (e.g. using __getitem__ and raising an IndexError)

Re: Recursive list comprehension

2004-12-08 Thread Terry Reedy
"Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > What is the getnext protocol? Is that the same thing that the iter() > docs call the sequence protocol? Yes. (I meant to write getitem rather than getnext.) > Because this definitely still works with itertools: Y

Re: Recursive list comprehension

2004-12-08 Thread Steven Bethard
Terry Reedy wrote: "Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Probably you want to catch a TypeError instead of an AttributeError; objects may support the iterator protocol without defining an __iter__ method: No, having an __iter__ method that returns an iter

Re: Recursive list comprehension

2004-12-08 Thread Terry Reedy
"Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Probably you want to catch a TypeError instead of an AttributeError; > objects may support the iterator protocol without defining an __iter__ > method: No, having an __iter__ method that returns an iterator is an es

Re: Recursive list comprehension

2004-12-08 Thread Steven Bethard
Adam DePrince wrote: On Wed, 2004-12-08 at 15:02, Steven Bethard wrote: Note that I special-case strings because, while strings support the iterator protocol, in this case we want to consider them 'atomic'. By catching the TypeError instead of an AttributeError, I can support old-style iterator

Re: Recursive list comprehension

2004-12-08 Thread Adam DePrince
On Wed, 2004-12-08 at 15:02, Steven Bethard wrote: > Adam DePrince wrote: > > def flatten( i ): > > try: > > i = i.__iter__() > > while 1: > > j = flatten( i.next() ) > > try: > > while 1: > > yield j.next() > >

Re: Recursive list comprehension

2004-12-08 Thread Steven Bethard
Peter Otten wrote: I noted that strings don't feature an __iter__ attribute. Therefore obj.__iter__() is not equivalent to iter(obj) for strings. Do you (plural) know whether this is a CPython implementation accident or can be relied upon? Nick Craig-Wood wrote: > With a little more investigation I

Re: Recursive list comprehension

2004-12-08 Thread Nick Craig-Wood
Adam DePrince <[EMAIL PROTECTED]> wrote: > def flatten( i ): > try: > i = i.__iter__() > while 1: > j = flatten( i.next() ) > try: > while 1: > yield j.next() > except StopIteration: >

Re: Recursive list comprehension

2004-12-08 Thread Peter Otten
Adam DePrince wrote: > def flatten( i ): > try: > i = i.__iter__() > while 1: > j = flatten( i.next() ) > try: > while 1: > yield j.next() > except StopIteration: > pass > except Attribu

Re: Recursive list comprehension

2004-12-08 Thread Steven Bethard
Adam DePrince wrote: def flatten( i ): try: i = i.__iter__() while 1: j = flatten( i.next() ) try: while 1: yield j.next() except StopIteration: pass except AttributeError: yield

Re: Recursive list comprehension

2004-12-08 Thread Adam DePrince
On Mon, 2004-12-06 at 10:01, Timothy Babytch wrote: > Serhiy Storchaka wrote: > > >>>sum([['N', 'F'], ['E'], ['D']], []) > ['N', 'F', 'E', 'D'] > > THE BEST! > > -- > Timothy Babytch, PHP-dev Teamleader Sum certainly takes the cake for hackish elegance, and would be my choice if I was absolut

Re: Recursive list comprehension

2004-12-06 Thread Peter Hansen
Timothy Babytch wrote: Serhiy Storchaka wrote: >>>sum([['N', 'F'], ['E'], ['D']], []) ['N', 'F', 'E', 'D'] THE BEST! Hmmm. Maybe, unless readability as in "self-documenting code" is important to you... Preceding the above with "flatten = sum" would perhaps be an adequate improvement. -Peter -- ht

Re: Recursive list comprehension

2004-12-06 Thread Timothy Babytch
Serhiy Storchaka wrote: >>>sum([['N', 'F'], ['E'], ['D']], []) ['N', 'F', 'E', 'D'] THE BEST! -- Timothy Babytch, PHP-dev Teamleader -- http://mail.python.org/mailman/listinfo/python-list

Re: Recursive list comprehension

2004-12-06 Thread Serhiy Storchaka
Timothy Babytch wrote: I have a list that looks like [['N', 'F'], ['E'], ['D']] I try to make it flat one: ['N', 'F', 'E', 'D'] How can I archieve such an effect with list comprehension? Two cycles did the job, but that way did not look pythonic.. I tried print [x for x in y for y in c_vars] and go

Re: Recursive list comprehension

2004-12-06 Thread Nick Coghlan
Peter Otten wrote: Nick Coghlan wrote: from itertools import chain n = [['N', 'F'], ['E'], ['D']] print [chain(*n)] However, [generator] is not the same as list(generator): Heh - good point. As you say, replacing with list() gives the intended answer. With regards to laziness, my main point was

Re: Recursive list comprehension

2004-12-06 Thread Peter Otten
Nick Coghlan wrote: > from itertools import chain > n = [['N', 'F'], ['E'], ['D']] > print [chain(*n)] However, [generator] is not the same as list(generator): >>> from itertools import chain >>> n = [['N', 'F'], ['E'], ['D']] >>> print [chain(*n)] [] >>> print list(chain(*n)) ['N', 'F', 'E', 'D

Re: Recursive list comprehension

2004-12-06 Thread Nick Coghlan
Peter Nuttall wrote: I think you do it with a generator like this: def flatten(nested): for sublist in nested: for element in sublist: yield element n=[['N', 'F'], ['E'], ['D']] output=[] for value in flatten(n): output.append(value) print output I highly recommend learning about the

Re: Recursive list comprehension

2004-12-06 Thread Peter Nuttall
On Monday 06 Dec 2004 09:26, Timothy Babytch wrote: > Hi all. > > I have a list that looks like [['N', 'F'], ['E'], ['D']] > I try to make it flat one: ['N', 'F', 'E', 'D'] > > How can I archieve such an effect with list comprehension? > Two cycles did the job, but that way did not look pythonic..

Re: Recursive list comprehension

2004-12-06 Thread Timothy Babytch
Peter Otten wrote: The order of the for expressions is as it would be for nested loops: items = [['N', 'F'], ['E'], ['D']] [y for x in items for y in x] I would still prefer a for loop because it spares you from iterating over the sublist items in python: data = [] for sub in [['N', 'F'], ['E'],

Re: Recursive list comprehension

2004-12-06 Thread Peter Otten
Timothy Babytch wrote: > Hi all. > > I have a list that looks like [['N', 'F'], ['E'], ['D']] > I try to make it flat one: ['N', 'F', 'E', 'D'] > > How can I archieve such an effect with list comprehension? > Two cycles did the job, but that way did not look pythonic.. > > I tried > print [x fo

Recursive list comprehension

2004-12-06 Thread Timothy Babytch
Hi all. I have a list that looks like [['N', 'F'], ['E'], ['D']] I try to make it flat one: ['N', 'F', 'E', 'D'] How can I archieve such an effect with list comprehension? Two cycles did the job, but that way did not look pythonic.. I tried print [x for x in y for y in c_vars] and got NameError: na