Here is a more complete answer. You should use itertools.combinations , not itertools.permutations , since the combination is very different from permutation.
For example, if you need all two combinations of array elements, such as [1,2,3,5] , the following code will give the result you want (equivalent to nchoosek in Matlab). Additional examples from this source .
>>> import itertools >>> all_combos = list(itertools.combinations([1,2,3,5], 2)) >>> print all_combos [(1, 2), (1, 3), (1, 5), (2, 3), (2, 5), (3, 5)]
if you want all combinations as a 2d array to just convert the list of tuples to a numpy array using the following command:
>>> all_combos = np.array(list(itertools.combinations([1,2,3,5], 2))) >>> print all_combos [[1 2] [1 3] [1 5] [2 3] [2 5] [3 5]]
source share