I want to get all possible combinations of three (or more) numbers. The numbers themselves need to be in the range of +-1. The range is to find 'similar numbers' - for example the number 3 needs to be iterated as 2,3,4. E.g I have:
num1 = 3
num2 = 4
num3 = 1
So in this example I want all combinations for these three numbers and every number +-1. (E.g. 341, 241, 441; 351, 331, ... ). So for the example numbers I should get 27 combinations.
First idea was to use 3 for-loops in python like this:
num1 = 3
num2 = 4
num3 = 1
def getSimilar(num1,num2,num3):
num1 = n1 - 2
for i in range (3):
num1 = num1 + 1
num2 = n2 - 2
for j in range(3):
num2 = num2 + 1
num3 = n3 - 2
for k in range(3):
num3 = num3 + 1
print(num1,num2,num3)
Output I get:
2 3 0
2 3 1
2 3 2
2 4 0
2 4 1
2 4 2
2 5 0
2 5 1
2 5 2
3 3 0
3 3 1
3 3 2
3 4 0
3 4 1
3 4 2
3 5 0
3 5 1
3 5 2
4 3 0
4 3 1
4 3 2
4 4 0
4 4 1
4 4 2
4 5 0
4 5 1
4 5 2
Is there a smarter and faster way to do this in python instead of using 3 for loops? The order of the output does not matter. A small problem I additionally have: If one number is 0, I need it to only iterate to 0 and 1, not to -1. So the output for 0; 4; 1 should be:
0 4 1
0 4 2
0 4 0
0 3 1
0 3 2
0 3 0
0 5 1
0 5 2
0 5 0
1 4 1
1 4 2
1 4 0
1 3 1
1 3 2
1 3 0
1 5 1
1 5 2
1 5 0
Python provides two keywords that terminate a loop iteration prematurely: The Python break statement immediately terminates a loop entirely. Program execution proceeds to the first statement following the loop body. The Python continue statement immediately terminates the current loop iteration.
In the context of most data science work, Python for loops are used to loop through an iterable object (like a list, tuple, set, etc.) and perform the same action for each entry. For example, a for loop would allow us to iterate through a list, performing the same action on each item in the list.
Here is a solution that handles your edge case:
from itertools import product
nums = [0, 4, 1]
options = [[x - 1, x, x + 1] for x in nums]
result = [similar for similar in product(*options) if all(x >= 0 for x in similar)]
for x in result:
print(x)
Output:
(0, 3, 0)
(0, 3, 1)
(0, 3, 2)
(0, 4, 0)
(0, 4, 1)
(0, 4, 2)
(0, 5, 0)
(0, 5, 1)
(0, 5, 2)
(1, 3, 0)
(1, 3, 1)
(1, 3, 2)
(1, 4, 0)
(1, 4, 1)
(1, 4, 2)
(1, 5, 0)
(1, 5, 1)
(1, 5, 2)
You can do that like this:
from itertools import product
def getSimilar(*nums):
return product(*(range(max(n - 1, 0), n + 2) for n in nums))
num1 = 3
num2 = 4
num3 = 1
for comb in getSimilar(num1, num2, num3):
print(comb)
# (2, 3, 0)
# (2, 3, 1)
# (2, 3, 2)
# (2, 4, 0)
# ...
Start by creating the list of valid digits via list comprehension, then use itertools.product to create the possible combinations
from itertools import product
digits = [3,4,0,-1]
#Generate all possible digits
all_digits = [ (k-1,k,k+1) for k in digits]
#Valid digits, ensuring not to consider negative digits
valid_digits = [digits for digits in all_digits if all(x >= 0 for x in digits)]
#Create the iterator of all possible numbers
nums = product(*valid_digits)
#Print the combinations
for num in nums:
print(*num)
The output will look like.
2 3
2 4
2 5
3 3
3 4
3 5
4 3
4 4
4 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With