A single python loop from zero to n to zero - python

Is there a Pythonesque way to create a loop that traverses a range from 0 to n and then back to 0?
I could just create 2 loops (one forward and one reverse), but I'm looking for single loop.
My goal is to have an infinite loop that counts from 0 to 1024 to 0.

You can use itertools for that:
If you want to go to 1024 and back once, you can use:
from itertools import chain
for i in chain(range(0,1024),range(1024,0,-1)):
print(i)
In case you will need this quite often, you can use a function to generate the iterable:
def range_back(start,end):
return chain(range(start,end),range(end,start,-1))
and use it like:
for i in range_back(0,1024):
print(i)
Or if you want to do this an infinite amount of times:
from itertools import chain, cycle
for i in cycle(chain(range(0,1024),range(1024,0,-1))):
print(i)

chain two iterables:
import itertools
for i in itertools.chain(range(1+n), reversed(range(n))):
do_whatever(i)

If you really want infinite loop:
start = 0
end = 1024
i = start
while True:
print(i)
if i==end:
increment = -1
elif i==start:
increment = 1
i += increment

In case you want a solution without using itertools:
while True:
for i in range(1025) + range(1025)[::-1]:
print i
Range is a generator in python 3, so cast it a list before adding or reversing it. Also, use parentheses on print i:
while True:
for i in list(range(1025)) + list(range(1025))[::-1]:
print(i)

Related

Save itertools product loop value and then resume from the last result

I'm using this code to break an simple password mycubana, but I want to "pause" the itertools loop so that, after some time, I could resume the loop starting from the last saved itertools.product value. Is there any way to do it without change a lot of the code ?
Code:
import string
from itertools import chain, product
def bruteforce(charset, minlenght, maxlenght):
return(''.join(candidate)
for candidate in chain.from_iterable(product(charset, repeat = i)
for i in range(minlenght, maxlenght)))
contador = 0
for attempt in bruteforce(string.ascii_lowercase, 8, 9):
codigo = 'mycubana'
contador+=1
if attempt==codigo:
print(attempt)
contador=0
Yes, it's actually pretty simple. Using your environment and the definition of bruteforce, the following code will perform two runs of 10 candidates each on the generated sequence:
bf26_8 = bruteforce(string.ascii_lowercase, 8, 9)
count = 0
for c in bf26_8:
count += 1
print(c)
if count == 10:
break
print("======== PAUSED ========")
for c in bf26_8:
count += 1
print(c)
if count == 20:
break
The "trick" is to store the result of bruteforce in a variable. That result is a generator, so if you iterate on it, but do not exhaust it (i.e., break the iteration loop), it will give you the continuations values once you start iterating again.

How to force a for loop counter to skip iterations in Python3?

I recently ran into an issue where I was using a for loop somewhat similar to this:
for i in range(lineCount(fileToBeProcessed)):
print(i)
j = doSomeStuff() #returns number of lines in the file to skip
i = i+j
print(i)
print('next_loop')
For a value of j={2,3,1} the output was:
1
3
next_loop
2
5
next_loop
.
.
My desired output:
1
3
next_loop
4
7
next_loop
.
.
Every time the next iteration started, the for loop counter i reset to the original cycle. My question is, is there a way to force the for loop to skip the iterations based on the return value j. I understand and was able to implement something similar with a while loop. However, I was curious as to how or why would Python not allow such manipulation?
It allows manipulations. But a for loop in Python works with a:
for <var> in <iterable>:
# ...
So Python does not attaches a special meaning to range(n) as a for loop: a range(n) is an iterable that iterates from 0 to n (exclusive). At the end of each iteration the next element of the iterable. It furthermore means that once you constructed a range(n), if you alter n, it has no impact on the for loop. This in contrast with for instance Java, where n is evaluated each iteration again.
Therefore you can manipulate the variable, but after the end of the loop, it will be assigned the next value of the loop.
In order to manipulate the variable, you can use a while loop:
i = 0 # initialization
while i < lineCount(fileToBeProcessed): # while loop
print(i)
j = doSomeStuff() #returns number of lines in the file to skip
i = i+j
print(i)
print('next_loop')
i += 1 # increment of the for loop is explicit here
Usually a while loop is considered to be "less safe" since you have to do the increment yourself (for all code paths in the loop). Since it is something one tends to forget, it is easier to write an endless loop.
Assuming that fileToBeProcessed is actually a file-like object, you can iterate directly over the file (i.e. over the lines in that file), or use enumerate(fileToBeProcessed) if you need the line numbers, and call next on that iterator to skip lines.
Like this (not tested):
iterator = enumerate(fileToBeProcessed) # or just iter = fileToBeProcessed
for i, line in iterator:
print(i)
j = doSomeStuff() #returns number of lines in the file to skip
for _ in range(j):
i, line = next(iterator) # advance iterator -> skip lines
print(i)
print('next_loop')
I've edited the code hope it may help
z =0
for i in range(lineCount(fileToBeProcessed)):
if i <= z: #if i is a value that you don't want to be output, then skip loop to next one
continue
print(i)
j = doSomeStuff()
cnt += 1
z = i+j #use a different variable from i since i the iterator value will not be updated
print(z)
print('next_loop')

How can I make this loop stop at a certain variable occurs

I need to write a script that generates random numbers between 1-257000 and stops when a certain number occurs telling me how many numbers it generated so far.
i manged to get this far but can't seem to get it to stop or count
x=1
while x < 257000:
import itertools
import random
def random_gen(low, high):
while True:
yield random.randrange(1, 257000)
gen = random_gen(1, 100)
items = list(itertools.islice(gen, 10))
print items
x = x+1
Thank you so much for your help
Huh. A few flaws (or at least unclear spots) in your code.
You run your loop max 257000 times. Even though the probability is low, there is a chance that you don't hit the number you seek in the loop.
Move your import statements out of your loop, no need to have python check loaded modules each round.
You use a generator for choices of a list (randrange) where you can simply use a randint() call.
You define a closed function within your loop which creates a new function at a new memory address each round.
You slice your results into lists of 10 elements each; is this for printing, or do you actually need your random integers grouped into such lists?
A very simple and straightforward implementation of your described problem could be:
import random
num = 0 # Our counter
certain_number = 123456 # The number we seek
while True: # Run until we break
# Increment for each new step
num += 1
# Generate a single number from the given range
random_number = random.randint(1, 257000)
if random_number == certain_number:
# Break if we hit it
break
print('Hit after {} tries.'.format(num))
>>> Hit after 382001 tries.
First, put your import statements and your function definitons outside your while-loop. That's being super redundant.
>>> def random_gen(low,high):
... while True:
... yield random.randrange(low,high)
...
>>> lucky = 7
>>> rg = random_gen()
>>> rg = random_gen(1,1000)
>>> next(itertools.dropwhile(lambda t: t[1] != lucky, enumerate(rg, 1)))
(811, 7)
>>>
Here's another run, just for fun:
>>> rg = random_gen(1,257000)
>>> n,L = next(itertools.dropwhile(lambda t: t[1] != lucky, enumerate(rg, 1)))
>>> n
22602
>>> L
7
>>>

How do I skip a few iterations in a for loop

In python I usually loop through ranges simply by
for i in range(100):
#do something
but now I want to skip a few steps in the loop. More specifically, I want something like continue(10) so that it would skip the whole loop and increase the counter by 10. If I were using a for loop in C I'd just sum 10 to i, but in Python that doesn't really work.
You cannot alter the target list (i in this case) of a for loop. Use a while loop instead:
while i < 10:
i += 1
if i == 2:
i += 3
Alternatively, use an iterable and increment that:
from itertools import islice
numbers = iter(range(10))
for i in numbers:
if i == 2:
next(islice(numbers, 3, 3), None) # consume 3
By assigning the result of iter() to a local variable, we can advance the loop sequence inside the loop using standard iteration tools (next(), or here, a shortened version of the itertools consume recipe). for normally calls iter() for us when looping over a iterator.
The best way is to assign the iterator a name - it is common have an iterable as opposed to an iterator (the difference being an iterable - for example a list - starts from the beginning each time you iterate over it). In this case, just use the iter() built-in function:
numbers = iter(range(100))
Then you can advance it inside the loop using the name. The best way to do this is with the itertools consume() recipe - as it is fast (it uses itertools functions to ensure the iteration happens in low-level code, making the process of consuming the values very fast, and avoids using up memory by storing the consumed values):
from itertools import islice
import collections
def consume(iterator, n):
"Advance the iterator n-steps ahead. If n is none, consume entirely."
# Use functions that consume iterators at C speed.
if n is None:
# feed the entire iterator into a zero-length deque
collections.deque(iterator, maxlen=0)
else:
# advance to the empty slice starting at position n
next(islice(iterator, n, n), None)
By doing this, you can do something like:
numbers = iter(range(100))
for i in numbers:
...
if some_check(i):
consume(numbers, 3) # Skip 3 ahead.
Why not just set the value to skip until? Like:
skip_until = 0
for i in range(100):
if i < skip_until:
continue
if SOME_CONDITION:
skip_until = i + 10
DO_SOMETHING()
where SOME_CONDITION is whatever causes you to skip and DO_SOMETHING() is the actual loop contents?
for i in range(0, 100, 10):
print(i)
will print 0, 10, 20 ...
Something that I keep using. You can use below to get a 10% prob on running your code:
import random
for i in range(1000000):
if random.random() > 0.1:
continue:
# your code
A charmed and simplest form is like that:
>>> for i in range(5,10):
... print (i)
...
5
6
7
8
9
Where 5 was the index that started iteration.

Looping from 1 to infinity in Python

In C, I would do this:
int i;
for (i = 0;; i++)
if (thereIsAReasonToBreak(i))
break;
How can I achieve something similar in Python?
Using itertools.count:
import itertools
for i in itertools.count(start=1):
if there_is_a_reason_to_break(i):
break
In Python 2, range() and xrange() were limited to sys.maxsize. In Python 3 range() can go much higher, though not to infinity:
import sys
for i in range(sys.maxsize**10): # you could go even higher if you really want
if there_is_a_reason_to_break(i):
break
So it's probably best to use count().
def to_infinity():
index = 0
while True:
yield index
index += 1
for i in to_infinity():
if i > 10:
break
Simplest and best:
i = 0
while not there_is_reason_to_break(i):
# some code here
i += 1
It may be tempting to choose the closest analogy to the C code possible in Python:
from itertools import count
for i in count():
if thereIsAReasonToBreak(i):
break
But beware, modifying i will not affect the flow of the loop as it would in C. Therefore, using a while loop is actually a more appropriate choice for porting that C code to Python.
Reiterating thg435's comment:
from itertools import takewhile, count
def thereIsAReasonToContinue(i):
return not thereIsAReasonToBreak(i)
for i in takewhile(thereIsAReasonToContinue, count()):
pass # or something else
Or perhaps more concisely:
from itertools import takewhile, count
for i in takewhile(lambda x : not thereIsAReasonToBreak(x), count()):
pass # or something else
takewhile imitates a "well-behaved" C for loop: you have a continuation condition, but you have a generator instead of an arbitrary expression. There are things you can do in a C for loop that are "badly behaved", such as modifying i in the loop body. It's possible to imitate those too using takewhile, if the generator is a closure over some local variable i that you then mess with. In a way, defining that closure makes it especially obvious that you're doing something potentially confusing with your control structure.
If you want to use a for loop, it's possible to combine built-in functions iter (see also this answer) and enumerate for an infinite for loop which has a counter. We're using iter to create an infinite iterator and enumerate provides the counting loop variable. The start value is zero by default, but you can set a different start value with the start argument.
for i, _ in enumerate(iter(bool, True), start=1):
input(i)
Which prints:
1
2
3
4
5
...
If you're doing that in C, then your judgement there is as cloudy as it would be in Python :-)
For a loop that exits on a simple condition check at the start of each iteration, it's more usual (and clearer, in my opinion) to just do that in the looping construct itself. In other words, something like (if you need i after loop end):
int i = 0;
while (! thereIsAReasonToBreak(i)) {
// do something
i++;
}
or (if i can be scoped to just the loop):
for (int i = 0; ! thereIsAReasonToBreak(i); ++i) {
// do something
}
That would translate to the Python equivalent:
i = 0
while not there_is_a_reason_to_break(i):
# do something
i += 1
Only if you need to exit in the middle of the loop somewhere (or if your condition is complex enough that it would render your looping statement far less readable) would you need to worry about breaking.
When your potential exit is a simple one at the start of the loop (as it appears to be here), it's usually better to encode the exit into the loop itself.
def infinity():
i=0
while True:
i+=1
yield i
for i in infinity():
if there_is_a_reason_to_break(i):
break
def natural_numbers():
yield from map(sum, enumerate(iter(int,1)))
for i in natural_numbers():
if there_is_a_reason_to_break(i):
break;

Categories