Re: multinomial combinations

2011-09-24 Thread Arnaud Delobelle
(sorry about the top posting)

Change yield c to yield (c,)

HTH

Arnaud

PS: you could also change the if ... To

If not ns:
Yield ((),)
Else:
...
On Sep 24, 2011 8:06 AM, "Dr. Phillip M. Feldman" <
phillip.m.feld...@gmail.com> wrote:
>
> I wrote a small generator function that produces multinomial combinations.

> (Python's itertools module does ordinary combinations, but not multinomial
> combinations). The code essentially works, except that the the last
> combination in each tuple is not enclosed in a nested tuple:
>
> In [2]: x= multinomial_combinations(range(7),[2,1,2])
>
> In [3]: x.next()
> Out[3]: ((0, 1), (2,), 3, 4)
>
> (The 3 and 4 should be enclosed in a nested tuple).
>
> Any suggestions as to what I'm doing wrong will be appreciated. My code
> follows:
>
> def multinomial_combinations(items, ns):
>
> if len(ns) == 1:
> for c in itertools.combinations(items, ns[0]):
> yield c
>
> else:
> for c_first in itertools.combinations(items, ns[0]):
> items_remaining= set(items) - set(c_first)
> for c_other in multinomial_combinations(items_remaining, ns[1:]):
> yield (c_first,) + c_other
> --
> View this message in context:
http://old.nabble.com/multinomial-combinations-tp32503896p32503896.html
> Sent from the Python - python-list mailing list archive at Nabble.com.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multinomial combinations

2011-09-24 Thread Chris Rebert
On Sat, Sep 24, 2011 at 12:06 AM, Dr. Phillip M. Feldman
 wrote:
>
> I wrote a small generator function that produces multinomial combinations.
> (Python's itertools module does ordinary combinations, but not multinomial
> combinations).  The code essentially works, except that the the last
> combination in each tuple is not enclosed in a nested tuple:
>
> In [2]: x= multinomial_combinations(range(7),[2,1,2])
>
> In [3]: x.next()
> Out[3]: ((0, 1), (2,), 3, 4)
>
> (The 3 and 4 should be enclosed in a nested tuple).
>
> Any suggestions as to what I'm doing wrong will be appreciated.  My code
> follows:
>
> def multinomial_combinations(items, ns):
>
>   if len(ns) == 1:
>      for c in itertools.combinations(items, ns[0]):
>         yield c

FWIW, changing the base case to:

if not ns:
yield ()

appears to fix the issue. (Disclaimer: Have not done additional testing.)

Cheers,
Chris

>   else:
>      for c_first in itertools.combinations(items, ns[0]):
>         items_remaining= set(items) - set(c_first)
>         for c_other in multinomial_combinations(items_remaining, ns[1:]):
>            yield (c_first,) + c_other
-- 
http://mail.python.org/mailman/listinfo/python-list


multinomial combinations

2011-09-24 Thread Dr. Phillip M. Feldman

I wrote a small generator function that produces multinomial combinations. 
(Python's itertools module does ordinary combinations, but not multinomial
combinations).  The code essentially works, except that the the last
combination in each tuple is not enclosed in a nested tuple:

In [2]: x= multinomial_combinations(range(7),[2,1,2])

In [3]: x.next()
Out[3]: ((0, 1), (2,), 3, 4)

(The 3 and 4 should be enclosed in a nested tuple).

Any suggestions as to what I'm doing wrong will be appreciated.  My code
follows:

def multinomial_combinations(items, ns):

   if len(ns) == 1:
  for c in itertools.combinations(items, ns[0]):
 yield c

   else:
  for c_first in itertools.combinations(items, ns[0]):
 items_remaining= set(items) - set(c_first)
 for c_other in multinomial_combinations(items_remaining, ns[1:]):
yield (c_first,) + c_other
-- 
View this message in context: 
http://old.nabble.com/multinomial-combinations-tp32503896p32503896.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list