迭代Python中的数组/列表以获取Alpha数字(Iterate Array/List in Python for Alpha Numeric)
什么是在3个独立的场合迭代(AZ + 0-9)的最佳方法。 例如AAA,AAB..AA0,AA1..ZZ0,ZZ1 ......一直到999.数字或字符是否出现在任一插槽中没有限制。
chars = [string.ascii_uppercase + string.digits] genned_chars = [] for char in list(itertools.product(''.join(char), repeat=3)): genned_chars.append(''.join(char))我从哪里去?
编辑:
while True: chars = string.ascii_uppercase + string.digits genned_chars = [''.join(i) for i in itertools .product(chars,repeat=3)] genned_chars = [''] session = requests.session() session.headers = { 'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53' } page = session.get('URL' + str(genned_chars)) if page.status_code == '302': continue else: with open("validurls.txt","a") as f: f.write( 'URL' + str(genned_chars) + '\n')What's the best method to iterate through (A-Z + 0-9) on 3 separate occasions that are all exclusive. For instance AAA, AAB..AA0, AA1..ZZ0, ZZ1...all the way to 999. There is no restriction in whether a number or character appears in either slot.
chars = [string.ascii_uppercase + string.digits] genned_chars = [] for char in list(itertools.product(''.join(char), repeat=3)): genned_chars.append(''.join(char))Where do I go from there?
EDIT:
while True: chars = string.ascii_uppercase + string.digits genned_chars = [''.join(i) for i in itertools .product(chars,repeat=3)] genned_chars = [''] session = requests.session() session.headers = { 'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53' } page = session.get('URL' + str(genned_chars)) if page.status_code == '302': continue else: with open("validurls.txt","a") as f: f.write( 'URL' + str(genned_chars) + '\n')最满意答案
你不需要将字符串放在列表中,也不要使用list作为生成器,并使用list comprehension来加入产品:
>>> import string >>> chars = string.ascii_uppercase + string.digits >>> genned_chars=[''.join(i) for i in itertools .product(chars,repeat=3)]You dont need to put the string in list and dont use list for an generator , and use a list comprehension for join the products :
>>> import string >>> chars = string.ascii_uppercase + string.digits >>> genned_chars=[''.join(i) for i in itertools .product(chars,repeat=3)]更多推荐
发布评论