Python double increment loop

I have a quick (and probably very trivial) question for most of you. I am trying to use a loop that will increment two variables, so I can create a heat map graph that shows the similarity of the files in a simple way.

The idea is that if I have 100 files, I would like to compare each of them with each other. I am currently repeating my comparisons (i.e. Compare files 1 and 2 and then file 2 and 1), which is very inefficient. The current abbreviated script is shown below.

 for fileX in range(1,4):
    for fileY in range(1,4):
        print "X is " + str(fileX) + ", Y is " + str(fileY)

The result I get looks something like this:

X is 1, Y is 1
X is 1, Y is 2
X is 1, Y is 3
X is 2, Y is 1
X is 2, Y is 2
X is 2, Y is 3
X is 3, Y is 1
X is 3, Y is 2
X is 3, Y is 3

While what I'm looking for looks something like this:

X is 1, Y is 1 << not necessary since it is always 100 %
X is 1, Y is 2
X is 1, Y is 3
X is 2, Y is 2 << not necessary since it is always 100 %
X is 2, Y is 3
X is 3, Y is 3 << not necessary since it is always 100 %

, 1 2, 1 3 2 3 . , , . , , , , (~ 500 . ).

.

+4
2

,

for fileX in range(1,4):
    for fileY in range(fileX,4):

equall,

for fileX in range(1,4):
    for fileY in range(fileX+1,4):
+6

. , itertools.combinations:

for fileX, fileY in itertools.combinations(range(1,4), 2):
    print "X is " + str(fileX) + ", Y is " + str(fileY)

:

X is 1, Y is 2
X is 1, Y is 3
X is 2, Y is 3

, ( , ) " " . , , .

+2

Source: https://habr.com/ru/post/1611293/


All Articles