How can I simplifiy this python iteration?

Related searches

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

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)
# ...

simplify a loop in python, Use list comprehensions. It's faster and cleaner. The interpreter can be slowed down a lot by the for loop, and there's nothing to lose by simply� An iterator is an object representing a stream of data. It does the iterating over an iterable. You can use an iterator to get the next value or to loop over it. Once, you loop over an iterator, there are no more stream values. Iterators use the lazy evaluation approach. Many built-in classes in Python are iterators.

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)

Python Single Line For Loops, Simplify your Python loops. If you're like most programmers, you know that, eventually, once you have an array, you're gonna have to write a� Python Iterators. An iterator is an object that contains a countable number of values. An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__().

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

5 Advanced Functions in Itertools To Simplify Iterations in Python , One essential coding principle is DRY (i.e., Don't Repeat Yourself), which applies to Python programming too. For example, let's suppose that� Iterator in Python is simply an object that can be iterated upon. An object which will return data, one element at a time. An object which will return data, one element at a time. Technically speaking, a Python iterator object must implement two special methods, __iter__() and __next__() , collectively called the iterator protocol .

7. Iteration — How to Think Like a Computer Scientist: Learning with , Because iteration is so common, Python provides several language features to This (simplified) description of the print function, shows another example of� If you need to iterate through a dictionary in Python and want it to be sorted by keys, then you can use your dictionary as an argument to sorted(). This will return a list containing the keys in sorted order, and you’ll be able to iterate through them:

You (Probably) Don't Need For-Loops | by Daw-Ran Liou, There are several ways to re-write for-loops in Python. Pause yourself when you have the urge to write a for-loop next time. Ask yourself, “Do I� Browse other questions tagged python-3.x coroutine synchronous or ask your own question. The Overflow Blog How we built it: our new Articles feature for Stack Overflow Teams

In Python, indefinite iteration is performed with a while loop. Here’s what you’ll cover in this tutorial: You’ll start with a comparison of some different paradigms used by programming languages to implement definite iteration. Then you will learn about iterables and iterators, two concepts that form the basis of definite iteration in Python.

Comments
  • So 3,4 and 1 are the digits of the number you want to create?
  • Possible duplicate of Get the cartesian product of a series of lists?
  • one thing i want to tell you is that printing stuff slows computation time.
  • @Hilea I know, that's just for debugging in my case at this point of my code. But thanks!
  • Fixed the bit about not producing negative numbers (didn't read that at first).
  • That's perfect for me! Thanks!
  • If it answers your question don't forget to mark it as an answer!
  • I know he didn't explicitly say it but what if I used a negative number in this solution?
  • Updated @Error-SyntacticalRemorse! Can you check and upvote if it looks good?