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
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)
"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
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
"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
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
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()
> >
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
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:
>
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
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
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
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
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
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
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
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
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
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..
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'],
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
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
22 matches
Mail list logo