Python/Python Programming

[Python] random 함수

Pydole 2020. 5. 13. 19:39

 

random.choice : 문자열, 리스트, 튜플과 같은 순서가 있는 반복개체에서 임의 요소를 리턴

 

# list

A = [1,2,3,4,5,6,7,8]

print(random.choice(A))

4



# Strings

A = '12345678'

print(random.choice(A))

3



# Tuple

A = (1,2,3,4,5,6,7,8)

print(random.choice(A))

6

 

 

random.shuffle : 순서 섞기

 

from random import shuffle

lst = [1,2,3]
shuffle(lst)
lst

[3, 1, 2]