Skip to content

Instantly share code, notes, and snippets.

@RodrigoCMoraes
Last active December 19, 2018 13:41
Show Gist options
  • Save RodrigoCMoraes/b8cc0be6259db787915419858c2a0734 to your computer and use it in GitHub Desktop.
Save RodrigoCMoraes/b8cc0be6259db787915419858c2a0734 to your computer and use it in GitHub Desktop.
Pairwise indexes from 1D array
def pairwise_indexes(array, mode='comb'):
"""
Get indexes pairwise from array
Params:
array(list or numpy.array): array with shape(N,) where N is array size
mode: comb=combinatations or perm=permutations
Returns:
pairs(numpy.array): array with pair elements
"""
import itertools as it
import numpy as np
array = np.array(array)
if mode == 'comb':
pairs = list(it.combinations(array, 2))
elif mode == 'perm':
pairs = list(it.permutations(array, 2))
pairs = np.array(pairs)
return pairs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment