Hack Forums
[Python] For loop question(skipping) - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] For loop question(skipping) (/showthread.php?tid=5818592)



For loop question(skipping) - jure1 - Mar 31, 2018

Hi guys

I'm new in Python so i have an question.
My code work but i know that it's very very bad way to do it.I don't know what is best way to skip big number in for loop

My code:

Code
first = ["a", "b", "c", "d", "e", "f", "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","!","-","_","0","1","2","3","4","5","6","7","8","9","."]

    keywords = map(''.join, product(first, repeat=x))
    for key in keywords:
        asd += 1
        if (asd > 100000000):

I'm doing this to skip for loop 100000000 times...It's bad,i know..But what is faster way to skip it? I need to skip big number,because i don't know other way..

Thank you


RE: For loop question(skipping) - .kr. - Mar 31, 2018

What is it for? What is it for?


RE: For loop question(skipping) - jure1 - Mar 31, 2018

I want to generate all possible character.But i want to skip some(because i'm generate it before..)


RE: For loop question(skipping) - Vitani - Mar 31, 2018

This is probably what you need:

PHP Code

<?php 
first 
= ["a", "b", "c", "d", "e", "f", "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","!","-","_","0","1","2","3","4","5","6","7","8","9","."]

keywords = map(''.join, product(first, repeat=x))
for 
key in keywords:
    
asd += 1
    
if asd <= len(first): 

This basically says that if asd is smaller or equal to the length of first (40), it'll do whatever you put below the if statement line.


RE: For loop question(skipping) - QGK - Apr 2, 2018

(Mar 31, 2018 10:37 AM)Vitani Wrote:  This is probably what you need:

PHP Code

<?php 
first 
= ["a", "b", "c", "d", "e", "f", "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","!","-","_","0","1","2","3","4","5","6","7","8","9","."]

keywords = map(''.join, product(first, repeat=x))
for 
key in keywords:
 
   asd += 1
    if asd 
<= len(first): 

This basically says that if asd is smaller or equal to the length of first (40), it'll do whatever you put below the if statement line.

I suppose so - the issue from OP is not really clear to me as to what he actually wants to do.

If all he has to do is check if the asd (basically an index now) is less than the length of the "first" list, it's unneccesary since the for loop already iterates over the list


RE: For loop question(skipping) - ChInfoOrg - Apr 3, 2018

Hello!
There are differents possibilities to do Something like you want. you can use for exemple the "string" module

with something like this (as basic).

>>> import string
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
>>>


RE: For loop question(skipping) - michalmonday - Apr 3, 2018

Code
my_list = ["first", "second", "third", "fourth"]
for item in my_list:
   print item
Outputs:
first
second
third
fourth

Code
my_list = ["first", "second", "third", "fourth"]
for item in my_list[2:]:
    print item
Outputs:
third
fourth

So apply the same to "keywords" list by saving the last checked keyword number (i) and then using it inside keywords[last_i_number:]. Save/keep track of last "asd" like:
Code
for key in keywords:
        asd += 1
        print("Last asd = " + str(asd))

And then reuse it like:

Code
for key in keywords[last_asd:]:

Or just save it to file and read it instead of changing the code manually each time, but that's the way in general


RE: For loop question(skipping) - vorticalbox - Apr 3, 2018

(Mar 31, 2018 10:37 AM)Vitani Wrote:  This is probably what you need:

PHP Code

<?php 
first 
= ["a", "b", "c", "d", "e", "f", "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","!","-","_","0","1","2","3","4","5","6","7","8","9","."]

keywords = map(''.join, product(first, repeat=x))
for 
key in keywords:
 
   asd += 1
    if asd 
<= len(first): 

This basically says that if asd is smaller or equal to the length of first (40), it'll do whatever you put below the if statement line.

wouldn't this store them all in keywords and then loop over them? should it no be something like this?

PHP Code

<?php 
first 
= ["a", "b", "c", "d", "e", "f", "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","!","-","_","0","1","2","3","4","5","6","7","8","9","."]
for 
key in map(''.join, product(first, repeat=x)):
 
   asd += 1
    if asd 
<= len(first):