Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > strange list comprehension on generator

Reply
Thread Tools

strange list comprehension on generator

 
 
Roland Puntaier
Guest
Posts: n/a
 
      08-29-2007
def changeOne(aa,idx):
aa[idx]=not aa[idx]
yield aa
for i in range(idx):
for x in changeOne(aa,i):
yield x

def changeOneOrder(aa):
yield aa
for i in range(len(aa)):
for x in changeOne(aa,i):
yield x

a=[False]*3
og=changeOneOrder(a)
#this does not return the way I would expect. why?
list(og)
#returns
#[[False, False, True], [False, False, True], [False, False, True],
[False, False, True], [False, False, True], [False, False, True], [False,
False, True], [False, False, True]]


#this works as expected
a=[False]*3
og=changeOneOrder(a)
og.next()
og.next()
....

#this works as expected
def ty():
yield 1
yield 2
yield 3

tg=ty()
list(tg)

cheers, Roland

 
Reply With Quote
 
 
 
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      08-29-2007
No greeting, no text? Pity.

Roland Puntaier wrote:

> def changeOne(aa,idx):
> aa[idx]=not aa[idx]
> yield aa
> for i in range(idx):
> for x in changeOne(aa,i):
> yield x
>
> def changeOneOrder(aa):
> yield aa
> for i in range(len(aa)):
> for x in changeOne(aa,i):
> yield x


Okay, two generator definitions.

> a=[False]*3
> og=changeOneOrder(a)
> #this does not return the way I would expect. why?


What do you expect? You created a generator object and bound it
to "og".

Please always state what you expect and what really happens. Fortune
telling is not one of my hobbies (and I think I'm not alone with
that).

Regards,


Björn

--
BOFH excuse #284:

Electrons on a bender

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      08-29-2007
> #this does not return the way I would expect. why?

You yield the very same list object all the times. So
when you make a later change, all earlier results will
get changed, too (since they are the same object).
Of course, it won't affect the terminal output, so you
don't see that the older values changed in the example
that you think works as expected.

HTH,
Martin
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      08-29-2007
> #this does not return the way I would expect. why?

You yield the very same list object all the times. So
when you make a later change, all earlier results will
get changed, too (since they are the same object).
Of course, it won't affect the terminal output, so you
don't see that the older values changed in the example
that you think works as expected.

HTH,
Martin
 
Reply With Quote
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 6:50 am, Roland Puntaier <Roland.Punta...@br-
automation.com> wrote:
> def changeOne(aa,idx):
> aa[idx]=not aa[idx]
> yield aa
> for i in range(idx):
> for x in changeOne(aa,i):
> yield x
>
> def changeOneOrder(aa):
> yield aa
> for i in range(len(aa)):
> for x in changeOne(aa,i):
> yield x
>
> a=[False]*3
> og=changeOneOrder(a)
> #this does not return the way I would expect. why?
> list(og)
> #returns
> #[[False, False, True], [False, False, True], [False, False, True],
> [False, False, True], [False, False, True], [False, False, True], [False,
> False, True], [False, False, True]]



If you want the "intermediate" states of 'a' in
list(og), then you need to yield a copy of that list,
instead of the list itself:

def changeOne(aa, idx):
aa[idx] = not aa[idx]
yield aa[:] # <------- a copy
for i in range(idx):
for x in changeOne(aa, i):
yield x

def changeOneOrder(aa):
yield aa[:] # <------- a copy
for i in range(len(aa)):
for x in changeOne(aa, i):
yield x

a = [False] * 3
og = changeOneOrder(a)
print list(og)

Otherwise, as you've already noticed, you can
loop over the iterator and do something
with the "instantaneous" state of 'a'.

--
Hope this helps,
Steven


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List comprehension vs generator expression memory allocation candide Python 3 09-20-2009 05:17 PM
List comprehension in if clause of another list comprehension Vedran Furac( Python 4 12-19-2008 01:35 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
I need a string/list/generator/comprehension incantation. Steven W. Orr Python 2 04-21-2007 02:03 AM
Strange (?) list comprehension behavior George Henry Python 3 07-20-2003 04:00 AM



Advertisments