Python combination and permuation code -
i have following code generate set of combination, append combination in list, , return list.
def make_combination(): import itertools max_range = 5 indexes = combinations_plus = [] in range(0, max_range): indexes.append(i) in xrange(2, max_range): each_combination = [list(x) x in itertools.combinations(indexes, i)] combinations_plus.append(each_combination) retrun combinations_plus it generates many combinations don't want (hard display). but, want following combination:
1) [[0, 1], [0, 2], [0, 3], [0, 4], [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
2) [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
3) [[0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [1, 2, 3, 4]]
i think problem in following line don't know is. idea mistake is.
combinations_plus.append(each_combination)
an easier way of doing want following:
list(list(itertools.combinations(list(range(5)), i)) in range(2, 5))
Comments
Post a Comment