Python 3 - counting with two different values

I am trying to figure out how to count to a certain integer (like a range) alternating between two different digits, for example 2 and 3. Thus, the output will be 2 5 7 10 12 15, etc.

I started trying to modify a simple while loop as follows to take two values:

a = 0 while a < 100: a = a + 2 + 3 print(a, end=' ') 

But it just ends up counting to 100 by 5.

I have tried number ranges for loops and modules like itertools to try to figure out a way to do this, and I'm completely at a dead end.

I did a search after searching on this subject, and everything I can find is counted by a single number with loops and ranges.

+7
python loops iteration
source share
10 answers

You can use itertools.cycle for this

 import itertools a = 0 it = itertools.cycle((2,3)) while a < 100: a += next(it) print(a) 

Exit

 2 5 7 10 ... 95 97 100 

The itertools.cycle generator will simply loop back the tuple in the tuple as many times as you call it.

+11
source share

You need to print the contents of a after adding two and after adding three:

 a = 0 while a < 100: a = a + 2 print(a, end=' ') if a < 100: a = a + 3 print(a, end=' ') 

With this, you can better build a generator that iteratively adds two and three alternations:

 def add_two_three(a): while True: a += 2 yield a a += 3 yield a 

Then you can print the contents of the generator to 100 or more:

 from itertools import takewhile print(' '.join(takewhile(lambda x: x < 100,add_two_three(0)))) 
+6
source share

You must increase a each time with a different value (2 and 3). You can simply swap the two values ​​used to increase a at each iteration to achieve this.

 a = 0 inc1 = 2 # values being used to increment a inc2 = 3 while a < 100: a = a + inc1 inc1, inc2 = inc2, inc1 # swap the values print(a, end=' ') 
+2
source share

Just for fun ...

 # Count from zero to 100, alternating between i and j i, j = 2, 3 k = i + j a = 0 while a < 100: a += a % k and j or i print(a, end=' ') print() 

Exit

 2 5 7 10 12 15 17 20 22 25 27 30 32 35 37 40 42 45 47 50 52 55 57 60 62 65 67 70 72 75 77 80 82 85 87 90 92 95 97 100 
+2
source share
 import numpy as np np.cumsum([2,3] * 20) 
+2
source share

If I understand you correctly, this should do this:

 a = 0 while a < 100: a = a + 2 print(a, end=' ') a = a + 3 print(a, end=' ') 

Instead of printing, you can add these values ​​to the list for later use.

0
source share
 def createRange(a,end,step1,step2): output=[] while a < end: a += step1 output.append(a) if a < end: a += step2 output.append(a) return output array = createRange(2,100,2,3) print array 
0
source share

Use the if as follows:

 a = 0 i = 0 while a < 100: if i % 2: a = a + 3 else: a = a + 2 i += 1 print(a, end=' ') 
0
source share

You could do more or less of what you are doing, just split additions 2 and 3:

 a = 0 while a < 100: a = a + 2 print(a, end=' ') if a > 97: # you don't actually need this because of how the numbers 2/3 work break # but this will check if a will be >100 after addition of the second number a = a +3 print(a, end=' ') 
0
source share

This short expression works:

 for a in (x // 10 for x in range(0,1000,25)): print(a) 

(but I can not recommend it)

0
source share

All Articles