This is my first python program. I use the below code to generate combinations for a given range.
for k in range(0, items+1):
for r in range(0, items+1):
if (r-k) > 0:
res = [x for x in itertools.combinations(range(k, r), r-k)]
print res
Let say items=4, the code produce 10 combinations
#
# [(0,)]
# [(0, 1)]
# [(0, 1, 2)]
# [(0, 1, 2, 3)]
# [(1,)]
# [(1, 2)]
# [(1, 2, 3)]
# [(2,)]
# [(2, 3)]
# [(3,)]
#
My questions are
(a) How can I retrieve each element in each combinations, let say, in [(1, 2, 3)], how can I retrieve value at offset 0 (i.e. 1) ?
(b) How can I store return value from itertools.combinations into a list array in "res" (eg, res[0] = [(0,)] , res[1] = [(0, 1)] ?
(c) Let say I want to use map(), How can I make the value eg [(0, 1)] as key, and assign a random value to this key?
print itertools.combinations(range(k, r), r - k)?itertools.combinationsreturns a generatorlist(itertools.combinations(range(k, r), r - k)), then.