Re: Generator question

2019-03-14 Thread Pierre Reinbold
Wow, thank you Ian for this very detailed answer, and thank you for taking the time for that! Much appreciated! If I get this right, I have to somehow fix the value of a_list during the loop, like when you use the classic default value argument trick with lambdas (damn, I should have thought of

Re: Generator question

2019-03-14 Thread Pierre Reinbold
Le 14/03/19 à 10:45, Peter Otten a écrit : > Pierre Reinbold wrote: > >> Wow, thank you Ian for this very detailed answer, and thank you for taking >> the time for that! Much appreciated! >> >> If I get this right, I have to somehow fix the value of a_list during the >> loop, like when you use

Re: Generator question

2019-03-14 Thread Peter Otten
Pierre Reinbold wrote: > Wow, thank you Ian for this very detailed answer, and thank you for taking > the time for that! Much appreciated! > > If I get this right, I have to somehow fix the value of a_list during the > loop, like when you use the classic default value argument trick with >

Re: Generator question

2019-03-14 Thread Pierre Reinbold
Wow, thank you Ian for this very detailed answer, and thank you for taking the time for that! Much appreciated! If I get this right, I have to somehow fix the value of a_list during the loop, like when you use the classic default value argument trick with lambdas (damn, I should have thought of

Re: Generator question

2019-03-13 Thread Ian Kelly
You're basically running into this: https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result To see why, let's try disassembling your function. I'm using Python 3.5 here, but it shouldn't make much of a difference. py>

Generator question

2019-03-13 Thread Pierre Reinbold
Dear all, I want to implement a function computing the Cartesian product if the elements of a list of lists, but using generator expressions. I know that it is already available in itertools but it is for the sake of understanding how things work. I already have a working recursive version, and

Re: basic generator question

2015-02-04 Thread Steven D'Aprano
Neal Becker wrote: I have an object that expects to call a callable to get a value: class obj: def __init__ (self, gen): self.gen = gen def __call__ (self): return self.gen() As written, there is no need for this obj class, it just adds a pointless layer of indirection.

Re: basic generator question

2015-02-04 Thread Ian Kelly
On Wed, Feb 4, 2015 at 6:23 AM, Neal Becker ndbeck...@gmail.com wrote: class rpt: def __init__ (self, value, rpt): self.value = value; self.rpt = rpt def __call__ (self): for i in range (self.rpt): yield self.value Note that this class is just reimplementing

Re: basic generator question

2015-02-04 Thread Peter Otten
Neal Becker wrote: I have an object that expects to call a callable to get a value: class obj: def __init__ (self, gen): self.gen = gen def __call__ (self): return self.gen() As written that looks a bit like if boolean_expression == True: ... as you could replace inst =

basic generator question

2015-02-04 Thread Neal Becker
I have an object that expects to call a callable to get a value: class obj: def __init__ (self, gen): self.gen = gen def __call__ (self): return self.gen() Now I want gen to be a callable that repeats N times. I'm thinking, this sounds perfect for yield class rpt: def __init__

Re: basic generator question

2015-02-04 Thread Chris Angelico
On Thu, Feb 5, 2015 at 12:23 AM, Neal Becker ndbeck...@gmail.com wrote: Now I want gen to be a callable that repeats N times. I'm thinking, this sounds perfect for yield class rpt: def __init__ (self, value, rpt): self.value = value; self.rpt = rpt def __call__ (self): for i

Re: basic generator question

2015-02-04 Thread Joel Goldstick
On Wed, Feb 4, 2015 at 9:32 AM, Chris Angelico ros...@gmail.com wrote: On Thu, Feb 5, 2015 at 12:23 AM, Neal Becker ndbeck...@gmail.com wrote: Now I want gen to be a callable that repeats N times. I'm thinking, this sounds perfect for yield class rpt: def __init__ (self, value,

Re: basic generator question

2015-02-04 Thread Joel Goldstick
On Wed, Feb 4, 2015 at 10:19 AM, Joel Goldstick joel.goldst...@gmail.com wrote: On Wed, Feb 4, 2015 at 9:32 AM, Chris Angelico ros...@gmail.com wrote: On Thu, Feb 5, 2015 at 12:23 AM, Neal Becker ndbeck...@gmail.com wrote: Now I want gen to be a callable that repeats N times. I'm

Re: Generator Question

2011-12-24 Thread GZ
I see. Thanks for the clarification. On Dec 22, 12:35 am, Steven D'Aprano steve +comp.lang.pyt...@pearwood.info wrote: On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: Now the question here is this: def h():     if condition=true:        #I would like to return an itereator with zero

Generator Question

2011-12-21 Thread GZ
Hi, I am wondering what would be the best way to return an iterator that has zero items. I just noticed the following two are different: def f(): pass def g(): if 0: yield 0 pass for x in f(): print x Traceback (most recent call last): File string, line 1, in fragment TypeError:

Re: Generator Question

2011-12-21 Thread Steven D'Aprano
On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: Hi, I am wondering what would be the best way to return an iterator that has zero items. return iter([]) I just noticed the following two are different: def f(): pass That creates a function that does nothing, and then returns None

Re: Generator Question

2011-12-21 Thread Chris Angelico
On Thu, Dec 22, 2011 at 4:45 PM, GZ zyzhu2...@gmail.com wrote: def h():    if condition=true:       #I would like to return an itereator with zero length    else:       for ...: yield x Easy. Just 'return' in the conditional. def h(): if condition: return

Re: Generator Question

2011-12-21 Thread Steven D'Aprano
On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: Now the question here is this: def h(): if condition=true: #I would like to return an itereator with zero length else: for ...: yield x In other words, when certain condition is met, I want to yield nothing. How to

Re: Generator question

2010-12-23 Thread Nobody
On Wed, 22 Dec 2010 15:49:31 -0800, Dan Stromberg wrote: def generator(): i = 0 while True: yield i i += 1 Shorter version: from itertools import count as generator -- http://mail.python.org/mailman/listinfo/python-list

Generator question

2010-12-22 Thread Victor Eijkhout
So I have a generator, either as a free function or in a class and I want to generate objects that are initialized from the generated things. def generator(): for whatever: yield something class Object(): def __init__(self): self.data = # the next

Re: Generator question

2010-12-22 Thread Emile van Sebille
On 12/22/2010 3:15 PM Victor Eijkhout said... So I have a generator, either as a free function or in a class and I want to generate objects that are initialized from the generated things. def generator(): for whatever: yield something class Object(): def

Re: Generator question

2010-12-22 Thread Dan Stromberg
On Wed, Dec 22, 2010 at 3:15 PM, Victor Eijkhout s...@sig.for.address wrote: So I have a generator, either as a free function or in a class and I want to generate objects that are initialized from the generated things. def generator():        for whatever:                yield something

Re: Generator question

2010-12-22 Thread Victor Eijkhout
Dan Stromberg drsali...@gmail.com wrote: You likely want a class variable: Sounds like an elegant solution. Thanks! Victor. -- Victor Eijkhout -- eijkhout at tacc utexas edu -- http://mail.python.org/mailman/listinfo/python-list

Re: dict generator question

2008-09-22 Thread Miles
On Fri, Sep 19, 2008 at 9:51 PM, Steven D'Aprano [EMAIL PROTECTED] wrote: Extending len() to support iterables sounds like a good idea, except that it's not. Here are two iterables: def yes(): # like the Unix yes command while True: yield y def rand(total): Return random

Re: dict generator question

2008-09-22 Thread bearophileHUGS
Steven D'Aprano: Extending len() to support iterables sounds like a good idea, except that it's not. Python language lately has shifted toward more and more usage of lazy iterables (see range lazy by default, etc). So they are now quite common. So extending len() to make it act like leniter()

Re: dict generator question

2008-09-22 Thread Steven D'Aprano
On Mon, 22 Sep 2008 04:21:12 -0700, bearophileHUGS wrote: Steven D'Aprano: Extending len() to support iterables sounds like a good idea, except that it's not. Python language lately has shifted toward more and more usage of lazy iterables (see range lazy by default, etc). So they are now

Re: dict generator question

2008-09-22 Thread bearophileHUGS
Steven D'Aprano: I'm sorry, I don't recognise leniter(). Did I miss something? I have removed the docstring/doctests: def leniter(iterator): if hasattr(iterator, __len__): return len(iterator) nelements = 0 for _ in iterator: nelements += 1 return nelements it

Re: dict generator question

2008-09-19 Thread Paul Rubin
Simon Mullis [EMAIL PROTECTED] writes: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] ... dict_of_counts = dict([(v[0:3], count) for v in l]) Untested: def major_version(version_string): convert '1.2.3.2' to '1.2' return '.'.join(version_string.split('.')[:2])

Re: dict generator question

2008-09-19 Thread Boris Borcic
Gerard flanagan wrote: George Sakkis wrote: .. Note that this works correctly only if the versions are already sorted by major version. Yes, I should have mentioned it. Here's a fuller example below. There's maybe better ways of sorting version numbers, but this is what I do. Indeed,

Re: dict generator question

2008-09-19 Thread Gerard flanagan
Boris Borcic wrote: Gerard flanagan wrote: George Sakkis wrote: .. Note that this works correctly only if the versions are already sorted by major version. Yes, I should have mentioned it. Here's a fuller example below. There's maybe better ways of sorting version numbers, but this is

Re: dict generator question

2008-09-19 Thread bearophileHUGS
Gerard flanagan: data.sort() datadict = \ dict((k, len(list(g))) for k,g in groupby(data, lambda s: '.'.join(s.split('.',2)[:2]))) That code may run correctly, but it's quite unreadable, while good Python programmers value high readability. So the right thing to do is to split that line

Re: dict generator question

2008-09-19 Thread MRAB
On Sep 19, 2:01 pm, [EMAIL PROTECTED] wrote: Gerard flanagan: data.sort() datadict = \ dict((k, len(list(g))) for k,g in groupby(data, lambda s:      '.'.join(s.split('.',2)[:2]))) That code may run correctly, but it's quite unreadable, while good Python programmers value high

Re: dict generator question

2008-09-19 Thread Steven D'Aprano
On Fri, 19 Sep 2008 17:00:56 -0700, MRAB wrote: Extending len() to support iterables sounds like a good idea, except that it could be misleading when: len(file(path)) returns the number of lines and /not/ the length in bytes as you might first think! Extending len() to support

Re: dict generator question

2008-09-19 Thread bearophileHUGS
MRAB: except that it could be misleading when: len(file(path)) returns the number of lines and /not/ the length in bytes as you might first think! :-) Well, file(...) returns an iterable of lines, so its len is the number of lines :-) I think I am able to always remember this fact.

dict generator question

2008-09-18 Thread Simon Mullis
Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version : count. (So, in this case: dict_of_counts = { 1.1 : 1, 1.2 : 2,

Re: dict generator question

2008-09-18 Thread marek . rocki
Simon Mullis napisał(a): Something like: dict_of_counts = dict([(v[0:3], count) for v in l]) I can't seem to figure out how to get count, as I cannot do x += 1 or x++ as x may or may not yet exist, and I haven't found a way to create default values. It seems to me that the count you're

Re: dict generator question

2008-09-18 Thread pruebauno
On Sep 18, 10:54 am, Simon Mullis [EMAIL PROTECTED] wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version : count. (So, in this case:

Re: dict generator question

2008-09-18 Thread George Sakkis
On Sep 18, 10:54 am, Simon Mullis [EMAIL PROTECTED] wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version : count. (So, in this case:

Re: dict generator question

2008-09-18 Thread Gerard flanagan
Simon Mullis wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version : count. (So, in this case: dict_of_counts = { 1.1 : 1,

Re: dict generator question

2008-09-18 Thread pruebauno
On Sep 18, 10:54 am, Simon Mullis [EMAIL PROTECTED] wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version : count. (So, in this case:

Re: dict generator question

2008-09-18 Thread George Sakkis
On Sep 18, 11:43 am, Gerard flanagan [EMAIL PROTECTED] wrote: Simon Mullis wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version :

Re: dict generator question

2008-09-18 Thread Simon Mullis
Haha! Thanks for all of the suggestions... (I love this list!) SM 2008/9/18 [EMAIL PROTECTED]: On Sep 18, 10:54 am, Simon Mullis [EMAIL PROTECTED] wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3,

Re: dict generator question

2008-09-18 Thread Gerard flanagan
George Sakkis wrote: On Sep 18, 11:43 am, Gerard flanagan [EMAIL PROTECTED] wrote: Simon Mullis wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with

Re: Generator question

2006-11-27 Thread Peter Otten
Mathias Panzenboeck wrote: Robert Kern wrote: Timothy Wu wrote: Hi, Using generator recursively is not doing what I expect: def test_gen(x): yield x x = x - 1 if x != 0: test_gen(x) The only thing that the last line does is *create* a new generator object. You

Generator question

2006-11-26 Thread Timothy Wu
Hi, Using generator recursively is not doing what I expect: def test_gen(x): yield x x = x - 1 if x != 0: test_gen(x) for item in test_gen(3): print item This gives me a single number 3 and not printing 2 and 1 as I would expect. What is wrong?? Timothy --

Re: Generator question

2006-11-26 Thread Robert Kern
Timothy Wu wrote: Hi, Using generator recursively is not doing what I expect: def test_gen(x): yield x x = x - 1 if x != 0: test_gen(x) The only thing that the last line does is *create* a new generator object. You need to actually iterate over it and yield its

Re: Generator question

2006-11-26 Thread Timothy Wu
On 11/26/06, Robert Kern [EMAIL PROTECTED] wrote: The only thing that the last line does is *create* a new generator object. You need to actually iterate over it and yield its values. E.g. In [2]: def test_gen(x): ...: yield x ...: x -= 1 ...: if x != 0: ...:

Re: Generator question

2006-11-26 Thread Mathias Panzenboeck
Robert Kern wrote: Timothy Wu wrote: Hi, Using generator recursively is not doing what I expect: def test_gen(x): yield x x = x - 1 if x != 0: test_gen(x) The only thing that the last line does is *create* a new generator object. You need to actually iterate over