473,388 Members | 1,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 1410
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
11
by: seannakasone | last post by:
Is there a way to get the callstack level in c++? for example, take the following code: void call3() { //callstack level would be 3 } void call2() { //callstack level would be 2 call3();
11
by: John Nagle | last post by:
The Python SSL object offers two methods from obtaining the info from an SSL certificate, "server()" and "issuer()". The actual values in the certificate are a series of name/value pairs in ASN.1...
1
by: dmitrey | last post by:
hi all, howto check is function capable of obtaining **kwargs? i.e. I have some funcs like def myfunc(a,b,c,...):... some like def myfunc(a,b,c,...,*args):... some like
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.