Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Generando una contraseña de 10 dígitos

Entonces, necesito generar una contraseña de 10 dígitos (necesita usar el módulo random ) que debe contener 2 letras minúsculas, 2 letras mayúsculas, 3 símbolos especiales y 3 números, todo en un orden aleatorio cada vez. He terminado la parte del generador de contraseñas aleatorias, pero no estoy seguro de cómo restringirlo a 2 letras minúsculas, 2 letras mayúsculas, 3 símbolos especiales y 3 números.

Esto es lo que tengo hasta ahora:

 import random import string lc_letter = ["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"] uc_letter = ["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"] symbols = ["!","@","#","$","%","^","&","*","(",")","_","+","=","-","/",">","<",",",".","?","\\"] numbers = ["0","1","2","3","4","5","6","7","8","9"] options = [lc_letter,uc_letter,symbols,numbers] for i in range(10): choice = random.choice(options) digit = random.choice(choice) print(digit, end = '')
over 4 years ago · Santiago Trujillo
6 answers
Answer question

0

Un enfoque alternativo que sugeriré es tomar 2 letras de mayúsculas, minúsculas, etc. y luego mezclar la contraseña resultante usando el método random.shuffle .

over 4 years ago · Santiago Trujillo Report

0

Elija todos los caracteres necesarios primero, luego barájelos:

 from random import choice as rd from random import shuffle import string lc_letter = ["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"] uc_letter = ["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"] symbols = ["!","@","#","$","%","^","&","*","(",")","_","+","=","-","/",">","<",",",".","?","\\"] numbers = ["0","1","2","3","4","5","6","7","8","9"] options = [ rd(lc_letter), rd(lc_letter), rd(uc_letter), rd(uc_letter), rd(symbols), rd(symbols), rd(symbols), rd(numbers), rd(numbers), rd(numbers), ] shuffle(options) print(''.join(options))
over 4 years ago · Santiago Trujillo Report

0

Puedes usar constantes de string :

 import random import string s = "" for i in range(2): s = s + random.choice(string.ascii_lowercase) for i in range(2): s = s + random.choice(string.ascii_uppercase) for i in range(3): s = s + random.choice(string.punctuation) for i in range(3): s = s + random.choice(string.digits) s = ''.join(random.sample(s, 10)) print(s)
over 4 years ago · Santiago Trujillo Report

0

Lo que realmente puedes hacer es hacer una lista de longitud 10 como esta:

 dist = [0, 0, 1, 1, 2, 2, 2, 3, 3, 3]

Esta lista representa la distribución de cada índice de su lista de options . Por ejemplo, coloca letras minúsculas primero en la opción y tiene que elegir 2 valores en minúscula, por lo tanto, hay 2 ceros en la lista de distribución.

Ahora puede elegir un índice en la lista:

 idx = random.randint(0, len(dist))

Luego, elija su elección de la lista en: options[dist[idx]] .

Por último, pop el valor en idx de dist .

 dist.pop(idx)

Esto generará todas las contraseñas válidas con la misma probabilidad.

over 4 years ago · Santiago Trujillo Report

0

Puede usar random.choice , random.sample y constantes del módulo de string para obtener contraseñas generadas aleatoriamente.

 import random import string lc_letter = string.ascii_lowercase uc_letter = string.ascii_uppercase # Could use string.punctuation here, but it would be different # as your list doesn't contain semicolons or colons, # while string.punctuation does. symbols = ["!","@","#","$","%","^","&","*","(",")","_","+","=","-","/",">","<",",",".","?","\\"] numbers = string.digits lc_selection = [random.choice(lc_letter) for _ in range(2)] uc_selection = [random.choice(uc_letter) for _ in range(2)] symbol_selection = [random.choice(symbols) for _ in range(3)] number_selection = [random.choice(numbers) for _ in range(3)] print(''.join(random.sample(lc_selection + uc_selection + symbol_selection + number_selection, 10)))
over 4 years ago · Santiago Trujillo Report

0

En mi opinión, una mejor versión de la solución de Yevgeniy Kosmak (la configuración independiente es más clara de ver, el bucle evita la duplicación de código y el uso de choices en lugar de choice evita un bucle).

 import random import string config = [ (2, string.ascii_lowercase), (2, string.ascii_uppercase), (3, string.punctuation), # or use your '!@#$%^&*()_+=-/><,.?\\' (3, string.digits), ] picked = [] for k, options in config: picked += random.choices(options, k=k) random.shuffle(picked) password = ''.join(picked) print(password)

¡Pruébelo en línea!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!