itertools query

Pradeep Patra smilesonisamal at gmail.com
Fri Sep 27 11:32:05 EDT 2019


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


 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