for source_i in sources:
for source_j in sources:
pass
This is the same thing as iterating through the pairs in the Cartesian product of sources
and itself. This can be written in one line by importing itertools
:
import itertools
for (i,j) in itertools.product(sources, repeat=2):
pass
Same pattern here:
for ni in i.nodes:
for nj in j.nodes:
pass
This can be rewritten as:
for (ni, nj) in itertools.product(i.nodes, j.nodes):
pass
So now you can nest them:
import itertools
for (i,j) in itertools.product(sources, repeat=2):
for (ni, nj) in itertools.product(i.nodes, j.nodes):
if ni != nj:
do_thing(ni, nj)