473,574 Members | 3,123 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help on generator...

Joh
hello,

i'm trying to understand how i could build following consecutive sets
from a root one using generator :

l = [1,2,3,4]

would like to produce :

[1,2], [2,3], [3,4], [1,2,3], [2,3,4]

but unfortunately can not, i guess i can do it by using sub generator
and maybe enumerate, please if you help could you explain a bit the
trick ? looks like this sub generator thing mess me up.

(i think it should may be also have [1,], [2,], [3,], [1,2,3,4] , and
then be filtered)

bests
Jul 18 '05 #1
45 3004
On 21 Jan 2005 05:58:03 -0800
jo******@yahoo. fr (Joh) wrote:
i'm trying to understand how i could build following consecutive sets
from a root one using generator :

l = [1,2,3,4]

would like to produce :

[1,2], [2,3], [3,4], [1,2,3], [2,3,4]

def consecutive_set s(l): .... for i in xrange(2, len(l)):
.... for j in xrange(0, len(l)-i+1):
.... yield l[j:j+i]
.... list(consecutiv e_sets([1,2,3,4]))

[[1, 2], [2, 3], [3, 4], [1, 2, 3], [2, 3, 4]]

--
Denis S. Otkidach
http://www.python.ru/ [ru]
Jul 18 '05 #2
Joh wrote:
hello,

i'm trying to understand how i could build following consecutive sets
from a root one using generator :

l = [1,2,3,4]

would like to produce :

[1,2], [2,3], [3,4], [1,2,3], [2,3,4]

Do you mean:

[1,2], [2,3], [3,4], [1,2,3], [2,3,4], [1,3,4]
(E.g. all elements in the power set except the empty set, the sets with
one element and the sets with all elements.)
Otherwise, please describe your desired sets in verbal - I cannot see
the point.

Jul 18 '05 #3
On Fri, 2005-01-21 at 17:14 +0300, Denis S. Otkidach wrote:
On 21 Jan 2005 05:58:03 -0800
jo******@yahoo. fr (Joh) wrote:
i'm trying to understand how i could build following consecutive sets
from a root one using generator :

l = [1,2,3,4]

would like to produce :

[1,2], [2,3], [3,4], [1,2,3], [2,3,4]

def consecutive_set s(l):

... for i in xrange(2, len(l)):
... for j in xrange(0, len(l)-i+1):
... yield l[j:j+i]


Since you have a much faster brain than I (though I ended up with
exactly the same thing barring variable names) and beat me to posting
the answer, I'll post the inevitable awful generator expression version
instead:

consecutive_set s = ( x[offset:offset+s ubset_size]
for subset_size in xrange(2, len(x))
for offset in xrange(0, len(x) + 1 - subset_size) )

--
Craig Ringer

Jul 18 '05 #4
Hi,

I recently read David Mertz (IBM DeveloperWorks) about generators and got
excited about using lazy constructs in my Python programming.

But besides the fact that generators are either produced with the new "yield"
reserved word or by defining the __new__ method in a class definition, I
don't know much about them.

In particular, I don't know what Python constructs does generate a generator.
I know this is now the case for reading lines in a file or with the new
"iterator" package. But what else ? Does Craig Ringer answer mean that list
comprehensions are lazy ? Where can I find a comprehensive list of all the
lazy constructions built in Python ? (I think that to easily distinguish lazy
from strict constructs is an absolute programmer need -- otherwise you always
end up wondering when is it that code is actually executed like in Haskell).

Thank you

Francis Girard
FRANCE

Le vendredi 21 Janvier 2005 15:38, Craig Ringer a écritÂ*:
On Fri, 2005-01-21 at 17:14 +0300, Denis S. Otkidach wrote:
On 21 Jan 2005 05:58:03 -0800

jo******@yahoo. fr (Joh) wrote:
i'm trying to understand how i could build following consecutive sets
from a root one using generator :

l = [1,2,3,4]

would like to produce :

[1,2], [2,3], [3,4], [1,2,3], [2,3,4]

>> def consecutive_set s(l):


... for i in xrange(2, len(l)):
... for j in xrange(0, len(l)-i+1):
... yield l[j:j+i]


Since you have a much faster brain than I (though I ended up with
exactly the same thing barring variable names) and beat me to posting
the answer, I'll post the inevitable awful generator expression version
instead:

consecutive_set s = ( x[offset:offset+s ubset_size]
for subset_size in xrange(2, len(x))
for offset in xrange(0, len(x) + 1 - subset_size) )

--
Craig Ringer


Jul 18 '05 #5
On Fri, 2005-01-21 at 22:38 +0800, Craig Ringer wrote:
consecutive_set s = ( x[offset:offset+s ubset_size]
for subset_size in xrange(2, len(x))
for offset in xrange(0, len(x) + 1 - subset_size) )


Where 'x' is list to operate on, as I should've initially noted. Sorry
for the reply-to-self.

I did say "awful" for a reason ;-)

--
Craig Ringer

Jul 18 '05 #6
Joh wrote:
i'm trying to understand how i could build following consecutive sets
from a root one using generator :

l = [1,2,3,4]

would like to produce :

[1,2], [2,3], [3,4], [1,2,3], [2,3,4]

but unfortunately can not, i guess i can do it by using sub generator
and maybe enumerate, please if you help could you explain a bit the
trick ? looks like this sub generator thing mess me up.


Here is an (untested) variant that accepts any iterable while trying to
remain memory-efficient. This makes it necessary to shuffle the order of
the output a bit.

from itertools import tee, islice

def gen(iterable, start, end):
it = iter(iterable)
while True:
it, a = tee(it)
a = tuple(islice(a, end-1))
for sz in xrange(start, len(a)+1):
yield a[:sz]
it.next()
if __name__ == "__main__":
print list(gen(range( 1, 5), 2, 4))

# prints:
# [(1, 2), (1, 2, 3), (2, 3), (2, 3, 4), (3, 4)]

Peter

Jul 18 '05 #7
On Fri, 2005-01-21 at 16:05 +0100, Francis Girard wrote:
I recently read David Mertz (IBM DeveloperWorks) about generators and
got excited about using lazy constructs in my Python programming.
Speaking of totally great articles, and indirectly to lazyness (though
not lazyily evaluated constructs), I was really impressed by this
fantastic article on metaclasses:

http://gnosis.cx/publish/programming/metaclass_1.html
http://gnosis.cx/publish/programming/metaclass_2.html

which shows that they're really just not that hard. That saved me an
IMMENSE amount of utterly tedious coding just recently.
But besides the fact that generators are either produced with the new
"yield" reserved word or by defining the __new__ method in a class
definition, I don't know much about them.
They can also be created with a generator expression under Python 2.4. A
generator expression works much like a list comprehension, but returns a
generator instead of a list, and is evaluated lazily. (It also doesn't
pollute the outside namespace with its working variables).
print [ x for x in range(1,10)] [1, 2, 3, 4, 5, 6, 7, 8, 9] print ( x for x in xrange(1,10) ) <generator object at 0x401e40ac> print list(( x for x in xrange(1,10) ))

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

Not the use of xrange above for efficiency in the generator expressions.
These examples are trivial and pointless, but hopefully get the point
across.
In particular, I don't know what Python constructs does generate a
generator.
As far as I know, functions that use yield, and generator expressions. I
was unaware of the ability to create them using a class with a __new__
method, and need to check that out - I can imagine situations in which
it might be rather handy.

I'm not sure how many Python built-in functions and library modules
return generators for things.
I know this is now the case for reading lines in a file or with the
new "iterator" package. But what else ? Does Craig Ringer answer mean
that list comprehensions are lazy ?
Nope, but generator expressions are, and they're pretty similar.
Where can I find a comprehensive list of all the lazy constructions
built in Python ? (I think that to easily distinguish lazy from strict
constructs is an absolute programmer need -- otherwise you always end
up wondering when is it that code is actually executed like in
Haskell).


I'm afraid I can't help you with that. I tend to take the view that side
effects in lazily executed code are a bad plan, and use lazy execution
for things where there is no reason to care when the code is executed.

--
Craig Ringer
Jul 18 '05 #8
Le vendredi 21 Janvier 2005 16:06, Craig Ringer a écritÂ*:
On Fri, 2005-01-21 at 22:38 +0800, Craig Ringer wrote:
consecutive_set s = ( x[offset:offset+s ubset_size]
for subset_size in xrange(2, len(x))
for offset in xrange(0, len(x) + 1 - subset_size) )


Where 'x' is list to operate on, as I should've initially noted. Sorry
for the reply-to-self.

I did say "awful" for a reason ;-)

--
Craig Ringer


First, I think that you mean :

consecutive_set s = [ x[offset:offset+s ubset_size]
for subset_size in xrange(2, len(x))
for offset in xrange(0, len(x) + 1 - subset_size)]

(with square brackets).

Second,

this is not lazy anymore (like Denis S. Otkidach previous answer was) because
the __whole__ list get constructed __before__ any other piece of code have a
chance to execute. The type of consecutive_set s is simply a list, not a
generator.

I'm just trying to understand and obviously I'm missing the point.

Thank you

Francis Girard
FRANCE

Jul 18 '05 #9
Really, thank you Craig Ringer for your great answer.

I'm afraid I can't help you with that. I tend to take the view that side
effects in lazily executed code are a bad plan, and use lazy execution
for things where there is no reason to care when the code is executed.

I completly agree with this. But this is much more true in theory than in
practice. In practice you might end up with big big memory usage with lazy
constructs as the "system" has to store intermediate results (the execution
context in the case of Python). These kinds of phenomena happen all the time
in a language like Haskell -- at least for a beginner like me -- if you don't
pay attention to it ; and this makes the language a lot more difficult to
master. Thus you have to keep an eye on performance even though, in FP, you
shoould just have to "declare" your intentions and let the system manage the
execution path.

http://gnosis.cx/publish/programming/metaclass_1.html
http://gnosis.cx/publish/programming/metaclass_2.html
Thank you, I'll read that.

Francis Girard
FRANCE

Le vendredi 21 Janvier 2005 16:42, Craig Ringer a écritÂ*: On Fri, 2005-01-21 at 16:05 +0100, Francis Girard wrote:
I recently read David Mertz (IBM DeveloperWorks) about generators and
got excited about using lazy constructs in my Python programming.


Speaking of totally great articles, and indirectly to lazyness (though
not lazyily evaluated constructs), I was really impressed by this
fantastic article on metaclasses:

http://gnosis.cx/publish/programming/metaclass_1.html
http://gnosis.cx/publish/programming/metaclass_2.html

which shows that they're really just not that hard. That saved me an
IMMENSE amount of utterly tedious coding just recently.
But besides the fact that generators are either produced with the new
"yield" reserved word or by defining the __new__ method in a class
definition, I don't know much about them.


They can also be created with a generator expression under Python 2.4. A
generator expression works much like a list comprehension, but returns a
generator instead of a list, and is evaluated lazily. (It also doesn't
pollute the outside namespace with its working variables).
print [ x for x in range(1,10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
print ( x for x in xrange(1,10) )
<generator object at 0x401e40ac>
print list(( x for x in xrange(1,10) ))


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

Not the use of xrange above for efficiency in the generator expressions.
These examples are trivial and pointless, but hopefully get the point
across.
In particular, I don't know what Python constructs does generate a
generator.


As far as I know, functions that use yield, and generator expressions. I
was unaware of the ability to create them using a class with a __new__
method, and need to check that out - I can imagine situations in which
it might be rather handy.

I'm not sure how many Python built-in functions and library modules
return generators for things.
I know this is now the case for reading lines in a file or with the
new "iterator" package. But what else ? Does Craig Ringer answer mean
that list comprehensions are lazy ?


Nope, but generator expressions are, and they're pretty similar.
Where can I find a comprehensive list of all the lazy constructions
built in Python ? (I think that to easily distinguish lazy from strict
constructs is an absolute programmer need -- otherwise you always end
up wondering when is it that code is actually executed like in
Haskell).


I'm afraid I can't help you with that. I tend to take the view that side
effects in lazily executed code are a bad plan, and use lazy execution
for things where there is no reason to care when the code is executed.

--
Craig Ringer


Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
2419
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next = gen.next() finally:
1
1279
by: Joh | last post by:
hello, thanks to all who replied to my post (2005-01-21) - (i can not post inside the original thread as i get "Unable to retrieve message csr7ep$3db$05$1@news.t-online.com" from googlenews :( > Do you mean: > , , , , , > (E.g. all elements in the power set except the empty set, the sets with > one element and the sets with all...
5
2243
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
0
3001
by: Limpor | last post by:
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can someone helps me. Thanks!! ----------------------------------------------------------------------- The code for the Stock class:...
0
2827
by: Limpor | last post by:
Hi, I’m working on a solitaire game as my course assignment, and I am having trouble to dealing with Stock class, which consists of an upturned top card plus deck. The code for Stock class: import java.util.*; import java.awt.*; /** * The Stock is the deck of unused cards. It is subtly different from the deck * itself in several ways....
4
1631
by: UMstudent | last post by:
I'm working on this program for class where I have to an amount of numbers that are randomly picked from 0 - 50. So what I have done is say there are 20 slots in my array so I'm trying to generator 20 random numbers for the array between 0-50 however I've ran into a few problems. Mainly figuring out how to make it print 20 seperate completely...
2
11103
by: masker | last post by:
I was on the web trying to find a javascript that would allow me to input a number on my website and have it increase by a given percentage every second. During my search I found the Earth Population Calculator on the javascripkit.com site. The Calculator functions just as I imagine I want my number generator to function, but I need it to...
5
11553
by: byahne | last post by:
We just went live today with a production SQL Server 2005 database running with our custom Java application. We are utilizing the jTDS open source driver. We migrated our existing application which was using InterBase over to SQL Server. To minimize the impact to our code, we created a stored procedure which would allow us to manage our...
26
266
by: Adem24 | last post by:
I need a good and fast random number generator (RNG), and a linear congruential generator (LCG), both with a max period >= 31 bits; the bigger the better. Additional requirements: - Must use integer-values only (32 or 64 bit), no floating point. - The RNG should have passed some statistical tests. - The "RAND_MAX" of these generators...
0
7842
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8110
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8273
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6514
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5658
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5336
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.