The idea is to print all the different combinations from a series of numbers in a list (order not important). This script will be used to automate the comparison of 40+ networks on a list :D
So, if we start with a series of 1 to 5, what we want to get is
1 2 1 3 2 3 1 4 2 4 3 4 1 5 2 5 3 5 4 5
So here is the python code
# This is the list with all the numbers numberList=[1,2,3,4,5] # This is where we find the length of the list (how many items are in there) listLength=len(numberList) # we begin the loop for the first digit for i in range(1,listLength): # we then loop the second digit for i2 in range(i,listLength): # we want to start with 0 so we do a -1 print numberList[i-1],numberList[i2]
which returns
1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5
just to make sure we got the right answer, we can turn it into a graph…and it should look like this (0 is 1, 1 is 2, and so on)…
One reply on “Mathematical Combination – Python”
[…] laziness, therefore, propelled me to write a Python script. I built the engine this morning, which calculates the different combinations (all 903 of […]