0

I have a 2D array and I need to do some analysis on it which involves me performing a calculation for every possible pair of elements and then summing them up.

The problem is that I need to avoid doing the calculation twice for each pair - if I am looking at elements A and B, I need to do the calculation on A*B and avoid repeating it for B*A.

At the moment I am doing it this way:

comb=[]
amo=len(inds)
for m in range(0,amo):
    for n in range(m+1,amo):
        comb.append([inds[m],inds[n]])

where indsis a 1d array.

Catherine Georgia
  • 879
  • 3
  • 13
  • 17
  • Have a look at: http://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays – tfeldmann Sep 02 '13 at 08:11
  • OK this does not solve my problem, this gives a combination of all elements but includes duplicates, which I need to avoid. I have edited my question to make this more clear. – Catherine Georgia Sep 02 '13 at 10:26

1 Answers1

0

Looks like you are looking for itertools.combinations.

from itertools import combinations
comb = list(combinations(inds, 2))
Shaac
  • 195
  • 14