itertools query
Peter Otten
__peter__ at web.de
Fri Sep 27 11:47:47 EDT 2019
Pradeep Patra wrote:
> Hi all,
>
> I have written a small program to generate all the combinations of a and b
> of the array. I want (6,7) tuple also included. Can anybody suggest what
> change I should make to get 6,7 included in my output? Any suggestions
The spec is not clear to me. If you don't care about the source list you can
use
list(all_subsets(a + b))
but that will include many more combinations, including those consisting of
three or four elements.
> Output:
> [(5,), (6,), (5, 6), (7,), (8,), (7, 8)]
>
> from itertools import chain, combinations
>
> a = [5,6]
> b = [7,8]
> ar=[]
> br=[]
>
> def all_subsets(ss):
> return chain(*map(lambda x: combinations(ss, x), range(1, len(ss)+1)))
>
> for subset in all_subsets(a):
> print(subset)
> ar.append(subset)
>
> for subset in all_subsets(b):
> print(subset)
> br.append(subset)
>
> fr=ar+br
> print(fr)
More information about the Python-list
mailing list