Re: Half-baked idea: list comprehensions with while
On 27/04/2012 5:57 a.m., Kiuhnm wrote: On 4/26/2012 19:48, Paul Rubin wrote: Roy Smithr...@panix.com writes: x = [a for a in iterable while a] from itertools import takewhile x = takewhile(bool, a) I see that as a 'temporary' solution, otherwise we wouldn't need 'if' inside of list comprehensions either. Kiuhnm We have if inside list comprehensions? I didn't know that, could you provide an example? -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On Fri, Apr 27, 2012 at 7:49 PM, Miles Rout miles.r...@gmail.com wrote: We have if inside list comprehensions? I didn't know that, could you provide an example? You mean like: [x*2+1 for x in range(10) if x%3] ? ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On 4/27/2012 11:49, Miles Rout wrote: On 27/04/2012 5:57 a.m., Kiuhnm wrote: On 4/26/2012 19:48, Paul Rubin wrote: Roy Smithr...@panix.com writes: x = [a for a in iterable while a] from itertools import takewhile x = takewhile(bool, a) I see that as a 'temporary' solution, otherwise we wouldn't need 'if' inside of list comprehensions either. Kiuhnm We have if inside list comprehensions? I didn't know that, could you provide an example? elems = [4, 1, 9, 6, 3, 8, 3, 9, 2] small_elems = [x for x in elems if x 5] print str(small_elems) # 4, 1, 3, 3, 2 #leading_small_elems = [x for x in elems while x 5] #print str(leading_small_elems) # 4, 1 Kiuhnm -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On Fri, 2012-04-27 at 19:57 +1000, Chris Angelico wrote: On Fri, Apr 27, 2012 at 7:49 PM, Miles Rout miles.r...@gmail.com wrote: We have if inside list comprehensions? I didn't know that, could you provide an example? You mean like: [x*2+1 for x in range(10) if x%3] Or like: print [ 0 if b%2==1 else 1 for b in range(10)] [1, 0, 1, 0, 1, 0, 1, 0, 1, 0] -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On Fri, Apr 27, 2012 at 8:37 PM, Tim Wintle tim.win...@teamrubber.com wrote: Or like: print [ 0 if b%2==1 else 1 for b in range(10)] [1, 0, 1, 0, 1, 0, 1, 0, 1, 0] That's nothing to do with the list comp, that's just the expression-if syntax that you can use anywhere. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On Fri, 27 Apr 2012 19:57:31 +1000 Chris Angelico ros...@gmail.com wrote: On Fri, Apr 27, 2012 at 7:49 PM, Miles Rout miles.r...@gmail.com wrote: We have if inside list comprehensions? I didn't know that, could you provide an example? You mean like: [x*2+1 for x in range(10) if x%3] Speaking of list comprehensions, if clauses and half-baked ideas, sometimes I'd like to be able to do something like: results = [x = expensive_call(i) for i in iterable if condition(x)] (or maybe expensive_call(i) as x... or whatever), instead of: results = [] for i in iterable: x = expensive_call(i) if condition(x): results.append(x) -- John -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On Fri, Apr 27, 2012 at 10:17 PM, John O'Hagan resea...@johnohagan.com wrote: results = [x = expensive_call(i) for i in iterable if condition(x)] Nest it: results = [x for x in (expensive_call(i) for i in iterable) if condition(x)] ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On 04/27/12 07:23, Chris Angelico wrote: On Fri, Apr 27, 2012 at 10:17 PM, John O'Haganresea...@johnohagan.com wrote: results = [x = expensive_call(i) for i in iterable if condition(x)] Nest it: results = [x for x in (expensive_call(i) for i in iterable) if condition(x)] While it's what I do in cases like this, the nesting is not nearly as readable as John's suggested syntax. Granted, the ability to do assignments in such a context opens up a whole can of unpythonic worms, so I'd suggest the following for clarity: temp = (expensive_call(i) for i in iterable) results = [x for x in temp if condition(x)] -tkc -- http://mail.python.org/mailman/listinfo/python-list
Half-baked idea: list comprehensions with while
I'm not seriously suggesting this as a language addition, just an interesting idea to simplify some code I'm writing now: x = [a for a in iterable while a] which equates to: x = [] for a in iterable: if not a: break x.append(a) It does has a few things going for it. It doesn't add any new keywords, nor does it change the meaning of any currently valid program. Whether it's sufficiently useful in general is another question :-) In the specific case I'm looking at now, I've got this annoying lump of code: valid_answers = [] for p in pairs: if not all(p): break valid_answers.append(p) which could be rewritten as: valid_answers = [p for p in pairs while all(p)] pairs is a list of tuples. I need the leading portion of the list where all elements of the tuple are string non-zero-length strings. Obviously, you'd do the corresponding generator expression as well. -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On Thu, Apr 26, 2012 at 10:02 AM, Roy Smith r...@panix.com wrote: I'm not seriously suggesting this as a language addition, just an interesting idea to simplify some code I'm writing now: x = [a for a in iterable while a] which equates to: x = [] for a in iterable: if not a: break x.append(a) Someone borrowed Guido's time machine: http://docs.python.org/library/itertools.html#itertools.takewhile Cheers, Chris -- http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On Thu, Apr 26, 2012 at 10:02 AM, Roy Smith r...@panix.com wrote: I'm not seriously suggesting this as a language addition, just an interesting idea to simplify some code I'm writing now: x = [a for a in iterable while a] which equates to: x = [] for a in iterable: if not a: break x.append(a) It does has a few things going for it. It doesn't add any new keywords, nor does it change the meaning of any currently valid program. Whether it's sufficiently useful in general is another question :-) In the specific case I'm looking at now, I've got this annoying lump of code: valid_answers = [] for p in pairs: if not all(p): break valid_answers.append(p) which could be rewritten as: valid_answers = [p for p in pairs while all(p)] pairs is a list of tuples. I need the leading portion of the list where all elements of the tuple are string non-zero-length strings. Obviously, you'd do the corresponding generator expression as well. It could also be written as: itertools.takewhile(all, pairs) http://docs.python.org/library/itertools.html#itertools.takewhile -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On Thu, Apr 26, 2012 at 11:02 AM, Roy Smith r...@panix.com wrote: I'm not seriously suggesting this as a language addition, just an interesting idea to simplify some code I'm writing now: x = [a for a in iterable while a] which equates to: x = [] for a in iterable: if not a: break x.append(a) It does has a few things going for it. It doesn't add any new keywords, nor does it change the meaning of any currently valid program. Whether it's sufficiently useful in general is another question :-) In the specific case I'm looking at now, I've got this annoying lump of code: valid_answers = [] for p in pairs: if not all(p): break valid_answers.append(p) which could be rewritten as: valid_answers = [p for p in pairs while all(p)] pairs is a list of tuples. I need the leading portion of the list where all elements of the tuple are string non-zero-length strings. Obviously, you'd do the corresponding generator expression as well. valid_answers = list(itertools.takewhile(all, pairs)) -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On 26/04/2012 18:02, Roy Smith wrote: I'm not seriously suggesting this as a language addition, just an interesting idea to simplify some code I'm writing now: x = [a for a in iterable while a] which equates to: x = [] for a in iterable: if not a: break x.append(a) It does has a few things going for it. It doesn't add any new keywords, nor does it change the meaning of any currently valid program. Whether it's sufficiently useful in general is another question :-) In the specific case I'm looking at now, I've got this annoying lump of code: valid_answers = [] for p in pairs: if not all(p): break valid_answers.append(p) which could be rewritten as: valid_answers = [p for p in pairs while all(p)] pairs is a list of tuples. I need the leading portion of the list where all elements of the tuple are string non-zero-length strings. Obviously, you'd do the corresponding generator expression as well. itertools.takewhile ? -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On 4/26/2012 19:02, Roy Smith wrote: I'm not seriously suggesting this as a language addition, just an interesting idea to simplify some code I'm writing now: x = [a for a in iterable while a] which equates to: x = [] for a in iterable: if not a: break x.append(a) It does has a few things going for it. It doesn't add any new keywords, nor does it change the meaning of any currently valid program. Whether it's sufficiently useful in general is another question :-) In the specific case I'm looking at now, I've got this annoying lump of code: valid_answers = [] for p in pairs: if not all(p): break valid_answers.append(p) which could be rewritten as: valid_answers = [p for p in pairs while all(p)] pairs is a list of tuples. I need the leading portion of the list where all elements of the tuple are string non-zero-length strings. Obviously, you'd do the corresponding generator expression as well. I think it's a nice idea. In the meantime, try this way: valid_answers = list(takewhile(all, pairs)) Kiuhnm -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
Roy Smith r...@panix.com writes: x = [a for a in iterable while a] from itertools import takewhile x = takewhile(bool, a) -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On 4/26/2012 19:48, Paul Rubin wrote: Roy Smithr...@panix.com writes: x = [a for a in iterable while a] from itertools import takewhile x = takewhile(bool, a) I see that as a 'temporary' solution, otherwise we wouldn't need 'if' inside of list comprehensions either. Kiuhnm -- http://mail.python.org/mailman/listinfo/python-list
Re: Half-baked idea: list comprehensions with while
On Thu, Apr 26, 2012 at 11:57 AM, Kiuhnm kiuhnm03.4t.yahoo...@mail.python.org wrote: On 4/26/2012 19:48, Paul Rubin wrote: Roy Smithr...@panix.com writes: x = [a for a in iterable while a] from itertools import takewhile x = takewhile(bool, a) I see that as a 'temporary' solution, otherwise we wouldn't need 'if' inside of list comprehensions either. The if clause is much more commonly used than while would be. I don't have any objections to the suggestion from a technical or syntactical perspective. I do however question whether the utility is great enough to be worthwhile from a long-term maintenance perspective. -- http://mail.python.org/mailman/listinfo/python-list