我想親自考慮這個問題但我知道這里有經驗豐富的人有很好的解決方案。我正在嘗試創建一個代碼編號生成器,我將對其進行改進以包括所有字母大小寫。但是我的問題是,比如一個8個字母的字串,我要復制for回圈八次,而且我不能通過設定一個數字來說出我想要多少個字串。現在想問一下有沒有解決方案可以防止for for 重復代碼,只能通過設定生成號來實作?
myPass = []
print("Calculate started..")
for a in string.digits:
for b in string.digits:
for c in string.digits:
for d in string.digits:
for e in string.digits:
for f in string.digits:
for g in string.digits:
for h in string.digits:
myPass.append(a b c d e f g h)
print("Calculate finish..")
例如,我想要一個函式,只需設定一個數字即可執行上述程序。這就是我可以調整字串數量的方法:
def Generate(lettersCount):
print("Generate for loops for 12 times..") # for e.g.
print("12 letters passwords calculated..") # for e.g.
Generate(12) # 12 for loop's generated..
請幫助我,任何幫助表示贊賞。
uj5u.com熱心網友回復:
你想要這樣的東西嗎?
import itertools as it
my_string = '1234'
s = it.permutations(my_string, len(my_string))
print([x for x in s])
輸出:[('1', '2', '3', '4'), ('1', '2', '4', '3'), ('1', '3', '2', '4'), ('1', '3', '4', '2'), ('1', '4', '2', '3'), ('1', '4', '3', '2'), ('2', '1', '3', '4'), ('2', '1', '4', '3'), ('2', '3', '1', '4'), ('2', '3', '4', '1'), ('2', '4', '1', '3'), ('2', '4', '3', '1'), ('3', '1', '2', '4'), ('3', '1', '4', '2'), ('3', '2', '1', '4'), ('3', '2', '4', '1'), ('3', '4', '1', '2'), ('3', '4', '2', '1'), ('4', '1', '2', '3'), ('4', '1', '3', '2'), ('4', '2', '1', '3'), ('4', '2', '3', '1'), ('4', '3', '1', '2'), ('4', '3', '2', '1')]
編輯:print(["".join(x) for x in s])如果要添加以獲取字串,請使用。輸出:['1234', '1243', '1324', '1342', '1423', '1432', '2134', '2143', '2314', '2341', '2413', '2431', '3124', '3142', '3214', '3241', '3412', '3421', '4123', '4132', '4213', '4231', '4312', '4321']
uj5u.com熱心網友回復:
您可以使遞回函式如下所示。
class PasswordGenerator():
def __init__(self):
self.password_list = []
def generate_password(self, len, added_string=""):
if len == 0:
self.password_list.append(added_string)
else:
for i in string.digits:
self.generate_rand_with_for(len - 1, i added_string)
然后你可以使用這個類來獲取密碼串列。
password_gen = PasswordGenerator()
password_gen.generate_password(12)
print(password_gen.password_list)
或者您可以使用 python 生成器來實作它。
import string
from random import choices
def generate_random_string(len):
while True:
yield ''.join(choices(string.ascii_letters string.digits, k = len))
gen = generate_random_string(12)
然后你可以隨時從這個生成器中得到一個字串。
print(next(gen))
或者您可以獲得任意數量的密碼,如下所示
number_of_passwords = 100000
for index, item in enumerate(gen_loop):
print(item)
if index == number_of_passwords:
break
希望它可以幫助。
uj5u.com熱心網友回復:
在 python 中有一個名為 ord() 的函式。此函式回傳字符的 unicode 值。從 0 到 9 的數字也是字符。我們可以查看從'0'到'9'字符的unicode值如下...
for c in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
print('Character : ', c, ' and Unicode value : ', ord(c))
你會得到一個這樣的輸出......
Character : 0 and Unicode value : 48
Character : 1 and Unicode value : 49
Character : 2 and Unicode value : 50
Character : 3 and Unicode value : 51
Character : 4 and Unicode value : 52
Character : 5 and Unicode value : 53
Character : 6 and Unicode value : 54
Character : 7 and Unicode value : 55
Character : 8 and Unicode value : 56
Character : 9 and Unicode value : 57
模塊“random”中有一個名為“randint()”的函式
random.randint(a, b)
Return a random integer N such that a <= N <= b. Alias for randrange(a, b 1).
現在考慮到您的 paawrod 將僅包含從“0”到“9”的數字,您可以使用以下代碼解決您的問題 ()...
def passwordGenerator(password_length):
password = ''
for length in range(password_length):
password = chr(random.randint(48, 57))
return password
print(passwordGenerator(12))
下面給出了生成密碼的幾個示例......
852501224302
501575191222
271006502875
914595005843
python 中的函式 chr() 從 unicode 值回傳字串表示形式。
uj5u.com熱心網友回復:
您是否正在嘗試制作密碼生成器?
這可以通過 random 模塊和單個 for 回圈來完成
all_symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
import random
def password_gen():
return ''.join(random.choice(all_symbols)for i in range(15))
password = password_gen()
print(f"Secure password - {password}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/476182.html
標籤:Python python-3.x for循环 发电机
