Re: Permutations using a recursive generator

2018-09-18 Thread Thomas Jollans
On 2018-09-18 17:05, ast wrote: > Le 18/09/2018 à 17:01, ast a écrit : >> Hello >> >> I found a smart and very concise code to >> generate all permutations of a list. >> I put it here if someone is interested to >> figure out how it works When you say "found a [...] code" I hope you mean "wrote a

Re: Permutations using a recursive generator

2018-09-18 Thread ast
Le 18/09/2018 à 17:01, ast a écrit : error: permut instead of S     yield from permut(li2, prefix+[elt]) -- https://mail.python.org/mailman/listinfo/python-list

Permutations using a recursive generator

2018-09-18 Thread ast
Hello I found a smart and very concise code to generate all permutations of a list. I put it here if someone is interested to figure out how it works def permut(li, prefix=[]): if len(li)==1: yield prefix + li else: for elt in li: li2 = li.copy()

Re: Recursive generator in Python 3.5

2016-11-02 Thread congloi1990
Thank you so much, this all clear for me now -- https://mail.python.org/mailman/listinfo/python-list

Re: Recursive generator in Python 3.5

2016-10-31 Thread Gregory Ewing
ne is just performing an early exit from the iteration. You could write it without any returns like this: def fg(args): if not args: yield "" else: for i in args[0]: for tmp in fg(args[1:]): yield i + tmp this is the Tower of Hanoi with recursive generator but i

Re: Recursive generator in Python 3.5

2016-10-31 Thread tpqnnd01
Thanks for your detail explanation, my problem may be the 'return' keyword. I confuse at the point that return come after yield keyword, I wonder what if in case this code do not have the 1st return. So, when i try to delete the 1st return, the code run with error (the list out of range). This

Re: Recursive generator in Python 3.5

2016-10-31 Thread Steve D'Aprano
On Tue, 1 Nov 2016 12:39 am, tpqnn...@gmail.com wrote: > I have some confuse about the recursive generator where the code mixing > Yield and return keywork as below. I understand that "return" keywork just > raise StopIteration exception but can not understanding in depth, s

Re: Recursive generator in Python 3.5

2016-10-31 Thread Chris Angelico
On Tue, Nov 1, 2016 at 1:50 AM, wrote: > > Please see the exampl I just got it below, this is the Tower of Hanoi with > recursive generator but it do not have the 'return' here, do you have the > advice for this term: > > def A001511(): > yield 1 &

Re: Recursive generator in Python 3.5

2016-10-31 Thread tpqnnd01
Hi Chris, Please see the exampl I just got it below, this is the Tower of Hanoi with recursive generator but it do not have the 'return' here, do you have the advice for this term: def A001511(): yield 1 b=1 for x in A001511(): yield x+1 yield 1 tri

Re: Recursive generator in Python 3.5

2016-10-31 Thread Chris Angelico
On Tue, Nov 1, 2016 at 1:03 AM, wrote: > Hi ChrisA, > > Thank you so much for sharing this point, I just concern to the point that: > normally, I found that the generator (it may be recursive generator also) do > not apply the "return" keyword, just the yield only.

Re: Recursive generator in Python 3.5

2016-10-31 Thread tpqnnd01
Hi ChrisA, Thank you so much for sharing this point, I just concern to the point that: normally, I found that the generator (it may be recursive generator also) do not apply the "return" keyword, just the yield only. But when I delete one or both of "return" keyword ab

Re: Recursive generator in Python 3.5

2016-10-31 Thread Chris Angelico
On Tue, Nov 1, 2016 at 12:39 AM, wrote: > I have some confuse about the recursive generator where the code mixing Yield > and return keywork as below. I understand that "return" keywork just raise > StopIteration exception but can not understanding in depth, so, could som

Recursive generator in Python 3.5

2016-10-31 Thread tpqnnd01
I have some confuse about the recursive generator where the code mixing Yield and return keywork as below. I understand that "return" keywork just raise StopIteration exception but can not understanding in depth, so, could some one explain me about the actual function of two "r

Re: Recursive generator for combinations of a multiset?

2013-12-05 Thread Dan Stromberg
On Wed, Nov 20, 2013 at 10:46 PM, John O'Hagan wrote: > > Short story: the subject says it all, so if you have an answer already, > fire away. Below is the long story of what I'm using it for, and why I > think it needs to be recursive. It may even be of more general > interest in terms of filteri

Re: Recursive generator for combinations of a multiset?

2013-11-27 Thread Oscar Benjamin
On 27 November 2013 10:25, John O'Hagan wrote: > On Tue, 26 Nov 2013 10:33:06 + > Oscar Benjamin wrote: > > I simplified it a bit more to this: > > def word_sequences(prepend, target, subwords): > """subwords is a list of lists of subwords grouped by length, > in order of length""

Re: Recursive generator for combinations of a multiset?

2013-11-27 Thread John O'Hagan
On Tue, 26 Nov 2013 10:33:06 + Oscar Benjamin wrote: > On 26 November 2013 06:18, John O'Hagan > wrote: > > [...] > > > > def _multicombs(prepend, words, r, chkstr): > > """chkstr is the string of remaining availalable characters""" > > if r == 0: > > yield prepend, chkstr >

Re: Recursive generator for combinations of a multiset?

2013-11-26 Thread Oscar Benjamin
On 26 November 2013 06:18, John O'Hagan wrote: > > Thanks, that is exactly the type of thing I was after. Although it is > actually slower as is than a hack like > > def imulticombs(it, n): > last = () > for i in itertools.combinations(it,n): > if i > last: > yield i >

Re: Recursive generator for combinations of a multiset?

2013-11-25 Thread John O'Hagan
On Mon, 25 Nov 2013 12:15:15 + Oscar Benjamin wrote: > On 21 November 2013 13:01, John O'Hagan > wrote: > > In my use-case the first argument to multicombs is a tuple of words > > which may contain duplicates, and it produces all unique > > combinations of a certain length of those words, eg

Re: Recursive generator for combinations of a multiset?

2013-11-25 Thread Oscar Benjamin
On 21 November 2013 13:01, John O'Hagan wrote: > In my use-case the first argument to multicombs is a tuple of words > which may contain duplicates, and it produces all unique combinations > of a certain length of those words, eg: > > list(multicombs(('cat', 'hat', 'in', 'the', 'the'), 3)) > > [('

Re: Recursive generator for combinations of a multiset?

2013-11-23 Thread John O'Hagan
On Fri, 22 Nov 2013 22:33:29 -0800 Dan Stromberg wrote: > On Fri, Nov 22, 2013 at 4:58 PM, John O'Hagan > wrote: > > > On Thu, 21 Nov 2013 12:59:26 -0800 > > Dan Stromberg wrote: > > > > > On Wed, Nov 20, 2013 at 10:46 PM, John O'Hagan > > > wrote: > > > > > > > > > > > Short story: the subject

Re: Recursive generator for combinations of a multiset?

2013-11-23 Thread John O'Hagan
On Sat, 23 Nov 2013 04:23:42 + MRAB wrote: > On 23/11/2013 00:58, John O'Hagan wrote: > > On Thu, 21 Nov 2013 12:59:26 -0800 > > Dan Stromberg wrote: > > > >> On Wed, Nov 20, 2013 at 10:46 PM, John O'Hagan > >> wrote: > >> > >> > > >> > Short story: the subject says it all, so if you have an

Re: Recursive generator for combinations of a multiset?

2013-11-22 Thread Dan Stromberg
On Fri, Nov 22, 2013 at 4:58 PM, John O'Hagan wrote: > On Thu, 21 Nov 2013 12:59:26 -0800 > Dan Stromberg wrote: > > > On Wed, Nov 20, 2013 at 10:46 PM, John O'Hagan > > wrote: > > > > > > > > Short story: the subject says it all, so if you have an answer > > > already, fire away. Below is the lo

Re: Recursive generator for combinations of a multiset?

2013-11-22 Thread MRAB
On 23/11/2013 00:58, John O'Hagan wrote: On Thu, 21 Nov 2013 12:59:26 -0800 Dan Stromberg wrote: On Wed, Nov 20, 2013 at 10:46 PM, John O'Hagan wrote: > > Short story: the subject says it all, so if you have an answer > already, fire away. Below is the long story of what I'm using it > for, a

Re: Recursive generator for combinations of a multiset?

2013-11-22 Thread John O'Hagan
On Thu, 21 Nov 2013 18:14:41 -0800 (PST) James wrote: > On Thursday, November 21, 2013 5:01:15 AM UTC-8, John O'Hagan wrote: [...] > > > On 21 November 2013 06:46, John O'Hagan > > > > > wrote: > > [...] > > > > > > def multicombs(it, r): > > > > > > result = it[:r] > > > > > >

Re: Recursive generator for combinations of a multiset?

2013-11-22 Thread John O'Hagan
On Thu, 21 Nov 2013 12:59:26 -0800 Dan Stromberg wrote: > On Wed, Nov 20, 2013 at 10:46 PM, John O'Hagan > wrote: > > > > > Short story: the subject says it all, so if you have an answer > > already, fire away. Below is the long story of what I'm using it > > for, and why I think it needs to be

Re: Recursive generator for combinations of a multiset?

2013-11-21 Thread James
On Thursday, November 21, 2013 5:01:15 AM UTC-8, John O'Hagan wrote: > On Thu, 21 Nov 2013 11:42:49 + > > Oscar Benjamin wrote: > > > > > On 21 November 2013 06:46, John O'Hagan > > > wrote: > > > > > > > > I found a verbal description of such an algorithm and came up with > > > > thi

Re: Recursive generator for combinations of a multiset?

2013-11-21 Thread John O'Hagan
On Thu, 21 Nov 2013 11:42:49 + Oscar Benjamin wrote: > On 21 November 2013 06:46, John O'Hagan > wrote: > > > > I found a verbal description of such an algorithm and came up with > > this: > > > > def multicombs(it, r): > > result = it[:r] > > yield result > > while 1: > >

Re: Recursive generator for combinations of a multiset?

2013-11-21 Thread Dan Stromberg
On Wed, Nov 20, 2013 at 10:46 PM, John O'Hagan wrote: > > Short story: the subject says it all, so if you have an answer already, > fire away. Below is the long story of what I'm using it for, and why I > think it needs to be recursive. It may even be of more general > interest in terms of filteri

Re: Recursive generator for combinations of a multiset?

2013-11-21 Thread Oscar Benjamin
On 21 November 2013 06:46, John O'Hagan wrote: > > I found a verbal description of such an algorithm and came up with > this: > > def multicombs(it, r): > result = it[:r] > yield result > while 1: > for i in range(-1, -r - 1, -1): > rep = result[i] > if

Recursive generator for combinations of a multiset?

2013-11-20 Thread John O'Hagan
e for medium-length inputs, much more for longer strings) by replacing itertools.product with this recursive generator: def cartesian_product(args, input_str): if not args: yield (), input_str return for words, check_str in cartesian_product(args[:-1], input_str):

Re: Recursive Generator Error?

2012-10-21 Thread Steven D'Aprano
On Sun, 21 Oct 2012 17:40:41 -0700, David wrote: > If I have one "yield" in function, the function will become generator, Almost correct. The function becomes a *generator function*, that is, a function that returns a generator object. Sometimes people abbreviate that to "generator", but that i

Re: Recursive Generator Error?

2012-10-21 Thread David
On Monday, October 22, 2012 7:59:53 AM UTC+8, Terry Reedy wrote: > On 10/21/2012 7:29 PM, David wrote: > > > I have a tree-like data structure, the basic elements are hash tables, > > > and they are grouped into lists, like [[{'a':1},[{'b':2}]]]. > > > And I want to flat the lists and visit hash

Re: Recursive Generator Error?

2012-10-21 Thread Terry Reedy
On 10/21/2012 7:29 PM, David wrote: I have a tree-like data structure, the basic elements are hash tables, and they are grouped into lists, like [[{'a':1},[{'b':2}]]]. And I want to flat the lists and visit hash table one by one, like {'a':1}, {'b':2}. But my program didn't work as I wish. When

Recursive Generator Error?

2012-10-21 Thread David
I have a tree-like data structure, the basic elements are hash tables, and they are grouped into lists, like [[{'a':1},[{'b':2}]]]. And I want to flat the lists and visit hash table one by one, like {'a':1}, {'b':2}. But my program didn't work as I wish. When it entered the 2nd flat_yield, it thre

Re: strange behavior from recursive generator

2011-09-23 Thread Phillip Feldman
I don't know how many times I stared at that code without seeing the error. Thanks so much! Phillip On Fri, Sep 23, 2011 at 1:26 PM, Arnaud Delobelle wrote: > On 23 September 2011 21:09, Dr. Phillip M. Feldman > wrote: > > > > A few weeks ago, I wrote a class that creates an iterator for solv

Re: strange behavior from recursive generator

2011-09-23 Thread Arnaud Delobelle
On 23 September 2011 21:09, Dr. Phillip M. Feldman wrote: > > A few weeks ago, I wrote a class that creates an iterator for solving the > general unlabeled-balls-in-labeled boxes occupancy problem. Chris Rebert > converted my code to a generator, which made the code cleaner, and I > subsequently s

strange behavior from recursive generator

2011-09-23 Thread Dr. Phillip M. Feldman
above, but one of them is now missing. If someone can shed light on why this is happening, I'd be grateful. Phillip http://old.nabble.com/file/p32503886/balls_in_labeled_boxes.py balls_in_labeled_boxes.py -- View this message in context: http://old.nabble.com/strange-behavior-from-recursive

Re: Recursive generator

2008-02-13 Thread Ben C
On 2008-02-13, Erich <[EMAIL PROTECTED]> wrote: > On Feb 12, 5:15 am, Ben C <[EMAIL PROTECTED]> wrote: >> I think this works OK, but it seems a bit odd. Is there something more >> "Pythonic" I should be doing? > > I have a similar tree to the one you describe here at work. I have a > visit function

Re: Recursive generator

2008-02-13 Thread Erich
On Feb 12, 5:15 am, Ben C <[EMAIL PROTECTED]> wrote: > I think this works OK, but it seems a bit odd. Is there something more > "Pythonic" I should be doing? I have a similar tree to the one you describe here at work. I have a visit function that is very similar to yours, but takes function argume

Re: Recursive generator

2008-02-13 Thread Ben C
On 2008-02-12, Paul Hankin <[EMAIL PROTECTED]> wrote: > On Feb 12, 10:17 pm, Ben C <[EMAIL PROTECTED]> wrote: >> On 2008-02-12, Paul Rubin <> wrote: >> >> > Paul Hankin <[EMAIL PROTECTED]> writes: >> >> def genDescendants(self): >> >>     return chain([self], *[child.genDescendants() >> >>        

Re: Recursive generator

2008-02-12 Thread Paul Hankin
On Feb 12, 10:17 pm, Ben C <[EMAIL PROTECTED]> wrote: > On 2008-02-12, Paul Rubin <> wrote: > > > Paul Hankin <[EMAIL PROTECTED]> writes: > >> def genDescendants(self): > >>     return chain([self], *[child.genDescendants() > >>         for child in self.children]) > > > That is scary.  It generate

Re: Recursive generator

2008-02-12 Thread Ben C
On 2008-02-12, Paul Rubin <> wrote: > Paul Hankin <[EMAIL PROTECTED]> writes: >> def genDescendants(self): >> return chain([self], *[child.genDescendants() >> for child in self.children]) > > That is scary. It generates an in-memory list the size of the > whole subtree, at every level.

Re: Recursive generator

2008-02-12 Thread Paul Rubin
Paul Hankin <[EMAIL PROTECTED]> writes: > def genDescendants(self): > return chain([self], *[child.genDescendants() > for child in self.children]) That is scary. It generates an in-memory list the size of the whole subtree, at every level. Total memory consumption is maybe even quadr

Re: Recursive generator

2008-02-12 Thread Ben C
On 2008-02-12, Paul Hankin <[EMAIL PROTECTED]> wrote: > On Feb 12, 11:15 am, Ben C <[EMAIL PROTECTED]> wrote: >> Suppose I have an object containing an array called children. I can >> therefore build a tree out of such objects. >> The best I came up with so far is : >> >>     def genDescendents(sel

Re: Recursive generator

2008-02-12 Thread Paul Hankin
On Feb 12, 11:15 am, Ben C <[EMAIL PROTECTED]> wrote: > Suppose I have an object containing an array called children. I can > therefore build a tree out of such objects. > The best I came up with so far is : > >     def genDescendents(self): >         for child in self.children: >             yield

Recursive generator

2008-02-12 Thread Ben C
Suppose I have an object containing an array called children. I can therefore build a tree out of such objects. I thought it might be useful to have a descendent generator, so I could write: for thing in self.genDescendents(): foo(thing) expecting foo to be called for each descendent

Re: performance of recursive generator

2005-08-11 Thread Steven Bethard
aurora wrote: > This test somehow water down the n^2 issue. The problem is in the depth > of recursion, in this case it is only log(n). It is probably more > interesting to test: > > def gen(n): > if n: > yield n > for i in gen(n-1): > yield i You should be

Re: performance of recursive generator

2005-08-11 Thread aurora
> You seem to be assuming that a yield statement and a function call are > equivalent. I'm not sure that's a valid assumption. I don't know. I was hoping the compiler can optimize away the chain of yields. > Anyway, here's some data to consider: > > test.py -

Re: performance of recursive generator

2005-08-11 Thread aurora
On Thu, 11 Aug 2005 01:18:11 -0700, Matt Hammond <[EMAIL PROTECTED]> wrote: > >> Is it an inherent issue in the use of recursive generator? Is there any >> compiler optimization possible? > > Hi, I could be misunderstanding it myself, but I think the short answer

Re: performance of recursive generator

2005-08-11 Thread Steven Bethard
aurora wrote: > I love generator and I use it a lot. Lately I've been writing some > recursive generator to traverse tree structures. After taking closer > look I have some concern on its performance. > > Let's take the inorder traversal from > http://www.python.

Re: performance of recursive generator

2005-08-11 Thread Terry Reedy
"aurora" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >I love generator and I use it a lot. Lately I've been writing some > recursive generator to traverse tree structures. After taking closer look > I have some concern on its performance. Wh

Re: performance of recursive generator

2005-08-11 Thread Duncan Booth
t issue in the use of recursive generator? Is there > any compiler optimization possible? > Do you actually have timing evidence that this is a problem with the data you are processing? The complexity of the algorithm may be important for very large data sets, but until you time it you won&#

Re: performance of recursive generator

2005-08-11 Thread Matt Hammond
> Is it an inherent issue in the use of recursive generator? Is there any > compiler optimization possible? Hi, I could be misunderstanding it myself, but I think the short answer to your question is that its an inherent limitation. > def inorder(t): > if t: > f

performance of recursive generator

2005-08-10 Thread aurora
I love generator and I use it a lot. Lately I've been writing some recursive generator to traverse tree structures. After taking closer look I have some concern on its performance. Let's take the inorder traversal from http://www.python.org/peps/pep-0255.html as an example. def