arnodel at gmail.com
Sat Sep 24 09:43:17 CEST 2011
(sorry about the top posting)
Change yield c to yield (c,)
PS: you could also change the if ... To
If not ns:
On Sep 24, 2011 8:06 AM, "Dr. Phillip M. Feldman" <
Phillip.M.Feldman at 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 : x= multinomial_combinations(range(7),[2,1,2])
> In : x.next()
> Out: ((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
> def multinomial_combinations(items, ns):
> if len(ns) == 1:
> for c in itertools.combinations(items, ns):
> yield c
> for c_first in itertools.combinations(items, ns):
> 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:
> Sent from the Python - python-list mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list