Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: looping list problem

Reply
Thread Tools

RE: looping list problem

 
 
Jon Bowlas
Guest
Posts: n/a
 
      08-16-2005
Ok so I changed it to this:

attobject = context.get_attobject()
navstring = context.get_uclattribute(attobject, 'ucl_navhide')
hiddennavelements = navstring.split(' ')
for hiddennavelement in hiddennavelements:
yield hiddennavelements

But I get the following error- Line 5: Yield statements are not allowed.

Any ideas

-----Original Message-----
From: python-list-bounces+me=(E-Mail Removed)
[mailtoython-list-bounces+me=(E-Mail Removed)] On Behalf Of
Fredrik Lundh
Sent: 16 August 2005 13:44
To: http://www.velocityreviews.com/forums/(E-Mail Removed)
Subject: Re: looping list problem

Jon Bowlas wrote:

> attobject = context.get_attobject()
> navstring = context.get_uclattribute(attobject, 'ucl_navhide')
> hiddennavelements = navstring.split(' ')
> for hiddennavelement in hiddennavelements:
> return hiddennavelement
>
> So the script 'get_attobject' basically looks for an instance of the
> attributes object in the current folder, if it doesn't locate one then it
> uses acquisition to find one in a parent folder. The script
> 'get_uclattribute' then gets the nodeValues of the requested node. In this
> instance its ucl_navhide, then I split the 'navstring' string at the

spaces
> and attempt the for-loop to output each of the values.
>
> Unfortunately it appears I am unable to loop through each of the list

items
> in hiddennavelements, as it only returns the first value & will not

repeat.

did you perhaps mean to use "yield" instead of "return" ?

</F>



--
http://mail.python.org/mailman/listinfo/python-list

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      08-16-2005
Jon Bowlas wrote:
> Ok so I changed it to this:
>
> attobject = context.get_attobject()
> navstring = context.get_uclattribute(attobject, 'ucl_navhide')
> hiddennavelements = navstring.split(' ')
> for hiddennavelement in hiddennavelements:
> yield hiddennavelements
>
> But I get the following error- Line 5: Yield statements are not allowed.


Please post the full traceback (cut and paste). Don't just retype the
error message like that.

-Peter
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      08-16-2005
Jon Bowlas wrote:

> Ok so I changed it to this:
>
> attobject = context.get_attobject()
> navstring = context.get_uclattribute(attobject, 'ucl_navhide')
> hiddennavelements = navstring.split(' ')
> for hiddennavelement in hiddennavelements:
> yield*hiddennavelements
>
> But I get the following error- Line 5: Yield statements are not allowed.


Please show us some more code -- especially the function containing and the
function calling the above chunk.

Generally speaking, a function terminates when execution reaches the first
return statement, e. g.

def f():
for i in 1, 2, 3:
return i

will always return 1. A generator, on the other hand,

def g():
for i in 1, 2, 3:
yield i

will yield 1, 2, and 3, but the calling code then needs itself a for loop:

for i in g():
# do something with i

My guess would be that you should either modify your for loop to

> attobject = context.get_attobject()
> navstring = context.get_uclattribute(attobject, 'ucl_navhide')
> hiddennavelements = navstring.split(' ')

for hiddennavelement in hiddennavelements:
# do something with hiddennavelement

or just return all elements at once

def some_func():
# ...
attobject = context.get_attobject()
navstring = context.get_uclattribute(attobject, 'ucl_navhide')
return navstring.split(' ')

and then operate on the items in the hiddennavelements list in the calling
code:

for element in some_func():
# do something with element

Peter










 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      08-17-2005
On Tue, 16 Aug 2005 14:24:55 +0100, "Jon Bowlas" <(E-Mail Removed)>
declaimed the following in comp.lang.python:


> for hiddennavelement in hiddennavelements:
> yield hiddennavelements
>

Well, off the top of my head... Shouldn't that yield refer to the
singular loop variable and not the plural list of items?
--
> ================================================== ============ <
> (E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      08-17-2005
On Tue, 16 Aug 2005 16:46:06 +0100, "Jon Bowlas" <(E-Mail Removed)>
declaimed the following in comp.lang.python:


Your indentation (at least as posted) is a shambles...
> for c in node.get_ordered_publishables():
> if not c.is_published():
> continue
> bert=c.getId();
> if bert=="index_right":
> continue
> if bert=="images":
> continue
> # this is where I loop through he elements returned by my hiddens function,
> comparing them with the value of bert
> for element in hiddens():


Going by the indentation here, the for loop is part of the "if
bert=='images':" block... But since the first statement in that block is
"continue", the for loop will never execute.

--
> ================================================== ============ <
> (E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
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
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
std::list: remove from front without deleting or looping through whole list Andy C++ 3 06-08-2007 09:02 PM
looping list problem Jon Bowlas Python 1 08-16-2005 01:34 PM
Re: looping through a list of lists. Rob Hunter Python 2 10-08-2003 05:31 PM
looping through a list of lists. saoirse_79 Python 0 10-08-2003 03:04 PM



Advertisments