• 회원가입
  • 로그인
  • 구글아이디로 로그인

[module] Python - random.sample() 메서드 ★ - 일부 요소 랜덤 선택. (= sample메서드 = 샘플)

1082  
목차
  1. random.sample() 예제  - 2개 요소 랜덤 선택
  2. random.sample() 정의
  3. random.sample() 구문

 

random.sample() 예제  - 2개 요소 랜덤 선택

 

import random

code = ["HTML", "CSS", "JS"]

print(random.sample(code, k=2))

 

결과값 예:  2개의 랜덤 요소.

['JS', 'HTML']

 

random.sample() 정의

 

시퀀스에서 지정 개수 만큼 랜덤 선.

 

※ 시퀀스: String, List, Tuple, Set, Range ...등.

원본 변경 X.

 

 

random.sample() 구문

 

random.sample(sequence, k)

 


[매개변수]

 

sequence

필수. 시퀀스 (예: String, List, Tuple, Set, Range... 등)

 

k

필수. 반환할 요소 개수.

 

PS. 아래 형식 둘 다 가능. 단, k대신 다른 문자 쓰면 에러 남.

random.sample(sequence, k=정수)

random.sample(sequence, 정수)

 


분류 제목
module Python - math.exp() 메서드 -
module Python - math.expm1() 메서드 -
module Python - math.fabs() 메서드 -
module Python - math.factorial() 메서드 -
module Python - math.floor() 메서드 ★ - 바닥 반올림 (= 하위 정수로 반올림. = floor메…
module Python - math.fmod() 메서드 -
module Python - math.frexp() 메서드 -
module Python - math.fsum() 메서드 -
module Python - math.gamma() 메서드 -
module Python - math.gcd() 메서드 -
module Python - math.hypot() 메서드 -
module Python - math.isclose() 메서드 -
module Python - math.isfinite() 메서드 - 숫자 유한 여부 체크 (= math.isfinite메…
module Python - math.isinf() 메서드 -
module Python - math.isnan() 메서드 ★ - NaN 여부 체크. (= isnan메서드 = 이즈난메서…
module Python - math.isqrt() 메서드 -
module Python - math.ldexp() 메서드 -
module Python - math.lgamma() 메서드 -
module Python - math.log() 메서드 -
module Python - math.log10() 메서드 -
4/7
목록
찾아주셔서 감사합니다. Since 2012