How to pass out the result from iterated function

James Stroud jstroud at mbi.ucla.edu
Wed Dec 10 16:38:49 EST 2008


JD wrote:
> I got a iterated function like this:
> 
> def iterSomething(list):
>     has_something = False
>     for cell in list:
>         if something in cell:
>             has_something = True
>             output = something
>    if has_something:
>        iterSomething(output)
>    else:
>        final_out = outupt
> 
> The problem is how can I read this final_out outside of the function.
> I tried the global statement, it seems not work. Any idea?
> 
> JD

I don't think this function will iterate the way you intend. You 
probably won't get many helpful suggestions until you define more 
precisely the nature of alist. Right now the function assumes single 
item lists only and will fail with an exception if something is not 
contained in the deepest list. E.g., only this type of situation is allowed:

  [[somethign]]

or

  [[[something]]]

or

  [[[[something]]]]

etc.

Anything else gives an exception.

For example, this will fail with an exception:

  [[1,2,3], [4,5,6], [7,8,9]]

Is that really what you want?

Hint: you need to test whether you can iterate over the elements alist.

Also, use something like "alist" and not "list" because "list" is a 
reserved word.

James



More information about the Python-list mailing list