생활
파이썬 배열 관련 질문입니다.
i = 0
while i < 5:
fruit_list = [apple, orange, grape, mango, pineapple]
fruit = fruit_list[i]
print(fruit)
i=i+1
이런 식으로 배열에 있는 과일들을 하나씩 출력해야 하는데 순서대로가 아닌
랜덤으로 출력하되 한번 출력됐던 과일은 출력하지 않게 하려면 어떻게 해야 하나요?
55글자 더 채워주세요.
2개의 답변이 있어요!
- from numpy import random import numpy as np arr = np.array(["apple", "orange", "grape", "mango", "pineapple"]) random.shuffle(arr) print(arr)
위와 같이 하면 과일이름을 랜덤으로 출력하게 됩니다
아래처럼 하면 될것 같습니다.
import random fruit_list = ['apple', 'orange', 'grape', 'mango', 'pineapple'] random.shuffle(fruit_list) for fruit in fruit_list: print(fruit)