Home > database >  How to select n elements from more than one list and create a new list with these elements?
How to select n elements from more than one list and create a new list with these elements?

Time:12-16

For example, I have this 3 lists:

letters = ['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']

numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

symbols = ['!', '#', '$', '%', '&', '(', ')', '*', ' ']

How do I select n elements from each of them and create a new list with these selected elements?

I'm trying to use for loops and random.choices(), but it's not working...

password_letters = [1]
for letter in range(nr_letters):
  password_letters = random.choice(letters)
  
password_numbers = 2
for number in range(nr_numbers):
  password_numbers = random.choice(numbers)

password_symbols = 3
for symbol in range(nr_symbols):
  password_symbols = random.choice(symbols)

password_final = [password_letters, password_numbers, password_symbols]
print(password_final, end = " ")

CodePudding user response:

The problem is that you are re-assigning the same variables over and over again, so after the end of each loop those will only retain the last assigned value. Either use random.choices() to extract more values at once (instead of random.choice()), or use lists/strings and append values to them:

Option 1:

# Notice the plural: random.choices()
password_letters = random.choices(letters, k=nr_letters)
password_numbers = random.choices(numbers, k=nr_numbers)
password_symbols = random.choices(symbols, k=nr_symbols)

Option 2:

password_letters = []
password_numbers = []
password_symbols = []

for _ in range(nr_letters):
    password_letters.append(random.choice(letters))

for _ in range(nr_numbers):
    password_numbers.append(random.choice(numbers))

for _ in range(nr_symbols):
    password_symbols.append(random.choice(symbols))

In both cases you will have 3 lists, so you can join them together with :

password_final = password_letters   password_numbers   password_symbols

Since all you are doing is putting everything into a single variable, you can just start with password_final = [] and .append() everything there.

And finally generate the password with the chosen characters after shuffling them around with random.shuffle():

random.shuffle(password_final)
password_str = ''.join(password_final)
print(password_str)

CodePudding user response:

if your purpose to generate password, you can try this also, considering you want 3 chars from each list.

import string
import random
password = []
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', ' ']
password.extend(random.choices(string.ascii_uppercase   string.ascii_lowercase,k=3))
password.extend(random.choices(string.digits, k=3))
password.extend(random.choices(symbols, k=3))
random.shuffle(password)
print(password)
#print(''.join(password)) # for making string instead of list

you'll get something like this, different for each run.

['&', '0', 'E', ')', '6', 't', 'k', '!', '0']
  • Related