- [python 3.10] 로또 번호 추출 하기
- 카테고리 없음
- 2023. 5. 30. 22:03
python 코드 몇 줄로 누구나 로또 번호 추출기를 만들 수 있습니다.
일단 python 3 를 설치하시고요.
# 중복 안되는 1~45 사이 6개 숫자 뽑기
# 중복 안되는 1~45 사이 6개 숫자 뽑기!!
import random
a = random.sample(range(1,45),6)
print(a)
# 결과창
[31, 17, 6, 23, 12, 42]
# 중복 안되는 1~45 사이 6개 숫자 뽑기 중 exceptList 의 숫자는 제외하고 추출하기
import random
exceptList = [3, 4, 6, 7, 10, 19, 22,23,24,32,45] # [] 안의 숫자는 빼고 번호 추출
totalList = range(1,45)
lastList = list(set(totalList)-set(exceptList))
resultList = random.sample(lastList,6)
print(resultList)
# 결과창
[9, 13, 38, 17, 20, 44]
# 중복 안되는 1~45 사이 6개 숫자 뽑기 중 이미 선택한 manList 의 숫자들은 빼고 추출하기 (로또 반자동)
import random
manList = [18,5] # manList 는 반드시 추출되는 번호에 포함 시키기.1개 ~ 5개 번호를 넣어도 됩니다.
totalList = range(1, 45)
lastList = list(set(totalList)-set(manList))
remainList = random.sample(lastList, 6-len(manList))
resultList = manList + remainList
print(resultList)
# 결과창
[18, 5, 16, 2, 30, 7]
# 중복 안되는 1~45 사이 6개 숫자 뽑기 중 반자동으로 하면서 자동 추출시 제외할 숫자 포함시키기
import random
manList = [18,5] # 반자동 선택한 숫자
exceptList = [3, 4, 6, 7, 10, 19, 22,23,24,32,45] # 자동 추출시 제외할 숫자
checkList = set(manList) - set(exceptList)
if len(checkList) != len(manList): # 반자동 포함된 숫자인데 제외할 숫자에도 있으면 오류
print("Check Mandatory list and Except list..")
return
totalList = range(1, 45)
lastList = list(set(totalList) - set(manList))
lastList2 = list(set(lastList) - set(exceptList))
remainList = random.sample(lastList2, 6 - len(manList))
resultList = manList + remainList
print("%s" % (resultList))
# 결과창
[18, 5, 15, 1, 34, 35]



Recent comment