-2

I have a folder with 300 txt files. lets say the .txt names are a, b, c, d etc. I need to compare each pair with a python script. I need to compare a-b, a-c,a-d, b-c, b-d. I do not want to have a-a and also i don't want to have both a-b and b-a. My guess was something like

for x in ['a', 'b', 'c', 'd']:
     for y in ['a', 'b', 'c', 'd']:
          if x != y:
            print(x , y)

but i am getting both a-b and b-a etc. If i scale it at the 300 file names i will get a few thousands duplicate outputs.

Any suggestions?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • You are talking about `combinations`, and there is a method to do this in [`itertools`](https://docs.python.org/3.5/library/itertools.html?highlight=combinations#itertools.combinations): `for x, y in itertools.combinations('abcd', 2):` – AChampion Nov 27 '16 at 17:06

1 Answers1

2

You can use combinations from itertools:

from itertools import combinations

files = ['a', 'b', 'c', 'd']
filesCombine = combinations(files, 2) # [('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
for f1, f2 in filesCombine:
    # compare f1 with f2

The second argument is the length of each combination, in this case 2