471,337 Members | 843 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

obtaining multiple values from a function.

hello all,

If I have a function that loops over a few elements and is expected to
throw out a few tuples as the output, then what should I be using in
place of return ?
Shriphani Palakodety.

Sep 25 '07 #1
5 1330
Shriphani wrote:
If I have a function that loops over a few elements and is expected to
throw out a few tuples as the output, then what should I be using in
place of return ?
Use a generator and have it /yield/ rather than /return/ results:
>>def f(items):
.... for item in items:
.... yield item.isdigit(), item.isupper()
....
>>for result in f("AAA bbb 111".split()):
.... print result
....
(False, True)
(False, False)
(True, False)

This is a very memory-efficient approach, particularly if the input items
are created on the fly, e. g. read from a file one at a time.

Peter
Sep 25 '07 #2
Shriphani <sh********@gmail.comwrites:
If I have a function that loops over a few elements and is expected to
throw out a few tuples as the output, then what should I be using in
place of return ?
If it makes sense for the set of results to be returned all at once,
then return the object that collects them all together — a list of
tuples, for example.
If, instead, it makes sense for the results to be iterated over, you
can write a function that yields results one at a time, without
necessarily knowing in advance what the entire set will be::
>>def fib(max_result):
... """ Yield numbers in the Fibonacci sequence
... to a maximum value of max_result. """
... prev_results = [0, 0]
... result = 1
... while result < max_result:
... yield result
... prev_results = [prev_results[1], result]
... result = sum(prev_results)
...

The function, when called, will return a generator object that you can
either iterate over::
>>fib_generator = fib(100)
for n in fib_generator:
... print n
...
1
1
2
3
5
8
13
21
34
55
89

or directly call its 'next' method to get one result at a time until
it raises a 'StopIteration' exception::
>>fib_generator = fib(5)
fib_generator.next()
1
>>fib_generator.next()
1
>>fib_generator.next()
2
>>fib_generator.next()
3
>>fib_generator.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration

--
\ "I have one rule to live by: Don't make it worse." -- Hazel |
`\ Woodcock |
_o__) |
Ben Finney
Sep 25 '07 #3
Ben Finney <bi****************@benfinney.id.auwrites:

>
If, instead, it makes sense for the results to be iterated over, you
can write a function that yields results one at a time, without
necessarily knowing in advance what the entire set will be::
>>def fib(max_result):
Going off on a tangent a bit, but I was idly considering the absence
of itertools.ireduce the other day. A problem is that reduce gives a
single result, so it's not clear what ireduce would do (perhaps why
it's not there). One obvious candidate would be to give the whole
sequence of partial reductions. I'm not sure if this would be
generally useful, but it would give a succinct way to do the Fibonacci
sequence:

itertools.ireduce(operator.add, itertools.count())

Sep 25 '07 #4
On Tue, 2007-09-25 at 10:41 +0100, Paul Rudin wrote:
Going off on a tangent a bit, but I was idly considering the absence
of itertools.ireduce the other day. A problem is that reduce gives a
single result, so it's not clear what ireduce would do (perhaps why
it's not there). One obvious candidate would be to give the whole
sequence of partial reductions. I'm not sure if this would be
generally useful, but it would give a succinct way to do the Fibonacci
sequence:

itertools.ireduce(operator.add, itertools.count())
Let's try it:
>>def ireduce(op, iterable, partial=0):
.... for nxt in iterable:
.... partial = op(partial, nxt)
.... yield partial
....
>>import operator
from itertools import islice, count

for x in islice(ireduce(operator.add, count()), 0, 10):
.... print x
....
0
1
3
6
10
15
21
28
36
45

That's not the Fibonacci sequence.

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 25 '07 #5
Carsten Haese <ca*****@uniqsys.comwrites:

...
That's not the Fibonacci sequence.
Bah, you're right of course, I should be more more careful before
posting these things.

Sep 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

66 posts views Thread by Darren Dale | last post: by
11 posts views Thread by seannakasone | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.