Генератор списков слов: Катастрофа после 2-го символаPython

Программы на Python
Ответить
Anonymous
 Генератор списков слов: Катастрофа после 2-го символа

Сообщение Anonymous »

Итак, это генератор списков слов, который я построил еще тогда, когда начал работать с Python (с тех пор, к сожалению, мало что сделал), и вчера, лежа в плохом состоянии, я понял, как я могу наконец починить свой алфавитный генератор, поскольку у меня были куча проблем с этим. Сейчас оно более-менее закончено, но есть только одна вещь, которая сводит меня с ума, взгляните:
# coding=utf8
# -*- coding: utf8 -*-
# vim: set fileencoding=utf8 :

# This is the number generator, the easy part

def number(length): # Digit generator
passw = 0
length = 10 ** length
for x in range(length):
f.write(str(passw)),
f.write("\n")
passw += 1
# print passw -1

"""Now this is the alphabetical generator. I am using a while loop to iterate
through the dictionary below. Tried it with a for loop first, but I ran into
some problems."""

def alpha(combinations):
durchgang = 0
truepw = ""
key = 0
currentalpha = -1
while key < combinations:
if key == 58:
combinations -= 58
currentalpha += 1
if currentalpha == 58:
currentalpha = 0
key = 0
truepw = letters[currentalpha]
continue
print truepw + letters[key]
# print key
key += 1

"""Now the problem I'm having, is that when I enter "2" as the length
of the password, everything works fine but as soon as I enter "3", I
just get the same again as with "2". Now I know this is because I am
overwriting "truepw" but I am just not sure how to add another "A" and then
"B" etc. before "truepw". The same with a length of "4" etc. I just have a
feeling I would need a million variables to which I always add the "new
truepw"."""

f = open('wordlist.txt', 'w')
length = int(raw_input("Define length of password: "))
combinations = 58 ** length
letters = {
0:"A",
1:"B",
2:"C",
3:"D",
4:"E",
5:"F",
6:"G",
7:"H",
8:"I",
9:"J",
10:"K",
11:"L",
12:"M",
13:"N",
14:"O",
15:"P",
16:"Q",
17:"R",
18:"S",
19:"T",
20:"U",
21:"V",
22:"W",
23:"X",
24:"Y",
25:"Z",
26:"Ä",
27:"Ö",
28:"Ü",
29:"a",
30:"b",
31:"c",
32:"d",
33:"e",
34:"f",
35:"g",
36:"h",
37:"i",
38:"j",
39:"k",
40:"l",
41:"m",
42:"n",
43:"o",
44:"p",
45:"q",
46:"r",
47:"s",
48:"t",
49:"u",
50:"v",
51:"w",
52:"x",
53:"y",
54:"z",
55:"ä",
56:"ö",
57:"ü"
}

# number(length)
alpha(combinations)

Надеюсь, вы, ребята, сможете мне как-нибудь помочь.
РЕДАКТИРОВАТЬ: Только что обнаружил это снова 9 лет спустя. Это было настоящее путешествие ностальгии, учитывая, что сейчас я работаю разработчиком Python. Ну, для тех, кто когда-либо нашел это и хочет получить ответ. Вот и все (очевидно, Python3 вместо Python2):
alpha_chars = [
"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",
"Ä",
"Ö",
"Ü",
"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",
"ä",
"ö",
"ü",
]

def number(length: int) -> list: # Digit generator
return [str(x) for x in range(10**length)]

def alpha_add(chars: list, position: int) -> list:
if position < 2:
return chars
alpha_chars = len([x for x in chars if len(x) == 1])
for c in chars:
if len(c) == position - 1:
chars += [c + x for x in chars[:alpha_chars]]
return chars

def alpha(length: int = 8, *args) -> list:
chars = [item for sublist in args for item in sublist]
for i in range(1, length + 1):
if i == 1:
continue
chars = alpha_add(chars, i)
return chars

pw_list = alpha(3, alpha_chars)

with open("pw_list.txt", "w") as f:
f.write("\n".join(pw_list))


Подробнее здесь: https://stackoverflow.com/questions/324 ... 2nd-symbol
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»