본문 바로가기
Python/Python

[Python_Bootcamp] 리스트 랜덤 추출

by HJ0216 2023. 8. 16.

이 글은 Dr. Angela Yu의 [Python_Bootcamp]를 수강하며 정리한 글입니다.

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#Password Generator Project
import random
 
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 = ['!''#''$''%''&''('')''*''+']
 
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
 
letter_list = [];
for i in range(0, nr_letters):
  random_num = random.randint(0len(letters)-1);
  letter_list.append(letters[random_num]);
 
symbol_list = [];
for i in range(0, nr_symbols):
  random_num = random.randint(0len(symbols)-1);
  symbol_list.append(symbols[random_num]);
 
number_list = [];
for i in range(0, nr_numbers):
  random_num = random.randint(0len(numbers)-1);
  number_list.append(numbers[random_num]);
 
all = letter_list+symbol_list+number_list;
 
random_list = []
 
while len(random_list) < len(all):
    all_len = len(all)
    random_num = random.randint(0, all_len - 1)
    if (random_list.count(random_num) == 0):
        random_list.append(random_num)
 
for i in random_list:
    print(all[i], end="")
 
 
 

1. while len(random_list) < len(all):

    - random_list가 all과 길이가 같아질 때까지 반복

 

2. if (random_list.count(random_num) == 0):

    - count()를 활용하여, 랜덤값 중 중복되지 않은 요소만 random_list에 추가

 

3. for i in random_list:

    - 중복이 제거된 random_list 값을 all에 대입해서 random하게 all list 출력

 

4. print(all[i], end="")

    - end=""를 통해 print 개행 제거

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#Password Generator Project
import random
 
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 = ['!''#''$''%''&''('')''*''+']
 
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
 
pwd_list = [];
 
for char in range(1, nr_letters + 1):
    random_char = random.choice(letters)
    pwd_list += random_char
 
for char in range(1, nr_symbols + 1):
    pwd_list += random.choice(symbols)
 
for char in range(1, nr_numbers + 1):
    pwd_list += random.choice(numbers)
 
random.shuffle(pwd_list);
 
pwd2 = "";
for char in pwd_list:
  pwd2 += char;
  
print(pwd2);
 
 
 

1. random_char = random.choice(letters)

    - choice(): 리스트에서 랜덤하게 요소를 반환

 

2. list에 요소를 추가하는 방법

    - pwd_list += random_char;

    - pwd_list.append(random_char);

 

3. random.shuffle(pwd_list)

    - shuffle(): 리스트 요소 셔플

 

4. pwd2 += char;

    - StringBuffer와 비슷하게 pwd_list 요소를 pwd2에 추가

 

'Python > Python' 카테고리의 다른 글

[Python_Bootcamp] Data Structures: List  (0) 2023.08.13
[Python_Bootcamp] f-String  (0) 2023.08.08