๐ ์ฐ์ต๋ฌธ์ (๋ฆฌ์คํธ)
- 1๋ถํฐ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ซ์๊น์ง์ ์ฝ์์ ์์๋ฅผ ๋ฆฌ์คํธ์ ๊ฐ๊ฐ ์ ์ฅํ๊ณ , ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์.
#1๋ถํฐ ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ์ซ์๊น์ง์ ์ฝ์์ ์์๋ฅผ ๋ฆฌ์คํธ์ ๊ฐ๊ฐ ์ ์ฅํ๊ณ , ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์.
inputNum = int(input('1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ : '))
listA = [] ; listB = []
for n in range(1, inputNum+1):
if n == 1:
listA.append(n)
else:
if inputNum % n == 0:
listA.append(n)
print('list A : {}'.format(listA))
for number in range(2, inputNum+1):
flag = True
for n in range(2, number):
if number % n == 0:
flag = False
break
if flag:
listB.append(number)
print('list B : {}'.format(listB))
>>>>
1๋ณด๋ค ํฐ ์ ์ ์
๋ ฅ : 40
list A : [1, 2, 4, 5, 8, 10, 20, 40]
list B : [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
๐ ์ฐ์ต๋ฌธ์ 2 (๋ฆฌ์คํธ)
- 1์ผ ์ ์ฒด ์ ์ฅ ์๊ธ์ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์.
import random
visitors = []
cnt_baby = [] ; cnt_kid = [] ; cnt_child = [] ; cnt_adult = [] ; cnt_grand = []
for n in range(100):
visitors.append(random.randint(1, 100))
for age in visitors:
if age <= 7 and age >= 0:
cnt_baby.append(age)
if age >= 8 and age <= 13:
cnt_kid.append(age)
if age >= 14 and age <= 19:
cnt_child.append(age)
if age >= 20 and age <= 64:
cnt_adult.append(age)
if age >= 65:
cnt_grand.append(age)
price_baby = (len(cnt_baby) * 0)
price_kid = (len(cnt_kid) * 200)
price_child = (len(cnt_child) * 300)
price_adult = (len(cnt_adult) * 500)
price_grand = (len(cnt_grand) * 0)
sum = price_baby + price_kid + price_child + price_adult + price_grand
print('-'*20)
print('์์ ์\t: {}๋ช
: {}์'.format(len(cnt_baby), format(price_baby, ',')))
print('์ด๋ฆฐ์ด\t: {}๋ช
: {}์'.format(len(cnt_kid), format(price_kid,',')))
print('์ฒญ์๋
\t: {}๋ช
: {}์'.format(len(cnt_child), format(price_child, ',')))
print('์ฑ์ธ\t\t: {}๋ช
: {}์'.format(len(cnt_adult), format(price_adult, ',')))
print('์ด๋ฅด์ \t: {}๋ช
: {}์'.format(len(cnt_grand), format(price_grand, ',')))
print('-'*20)
print('1์ผ ์๊ธ ์ด ํฉ๊ณ : {}์'.format(format(sum,',')))
>>>>
--------------------
์์ ์ : 6๋ช
: 0์
์ด๋ฆฐ์ด : 6๋ช
: 1,200์
์ฒญ์๋
: 6๋ช
: 1,800์
์ฑ์ธ : 43๋ช
: 21,500์
์ด๋ฅด์ : 39๋ช
: 0์
--------------------
1์ผ ์๊ธ ์ด ํฉ๊ณ : 24,500์
๐ ์ฐ์ต๋ฌธ์ 3 (๋ฆฌ์คํธ)
- ๋ค์ ๋ฆฌ์คํธ์์ ์ค๋ณต ์์ดํ (์ซ์)์ ์ ๊ฑฐํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์.
#๋ค์ ๋ฆฌ์คํธ์์ ์ค๋ณต ์์ดํ
(์ซ์)์ ์ ๊ฑฐํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์
numbers = [2, 22, 7, 8, 9, 2, 7, 3, 5, 2, 7, 1, 3]
print('numbers : {}'.format(numbers))
idx = 0
while True:
if idx >= len(numbers): #idx๊ฐ numbers ๋ฆฌ์คํธ ๊ฐ์๋ณด๋ค ๋ง์์ง๋ฉด ๋ฉ์ถฐ๋ผ
break
if numbers.count(numbers[idx]) >= 2: #idx์๋ฆฌ์ ์๋ numbers์ ๊ฐ์๊ฐ 2๊ฐ ์ด์์ด๋ผ๋ฉด
numbers.remove(numbers[idx]) : #idx์๋ฆฌ์ ์๋ numbers๋ฅผ ์ง์๋ผ
continue
idx+=1
print('numbers : {}'.format(numbers))
>>>>
numbers : [2, 22, 7, 8, 9, 2, 7, 3, 5, 2, 7, 1, 3]
numbers : [22, 8, 9, 5, 2, 7, 1, 3]
๐ ์ฐ์ต๋ฌธ์ 4 (๋ฆฌ์คํธ)
- 4๊ฐ์ ์ซ์ ์ค ์๋ก ๋ค๋ฅธ ์ซ์ 2๊ฐ๋ฅผ ์ ํํด์ ๋ง๋ค ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์
#4๊ฐ์ ์ซ์ ์ค ์๋ก ๋ค๋ฅธ ์ซ์ 2๊ฐ๋ฅผ ์ ํํด์ ๋ง๋ค ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์
numbers = [4, 6, 7, 9]
result = []
for number1 in numbers:
for number2 in numbers:
if number1 == number2:
continue
result.append([number1, number2])
print(f'result = {result}')
print(f'result length = {len(result)}')
>>>>
result = [[4, 6], [4, 7], [4, 9], [6, 4], [6, 7], [6, 9], [7, 4], [7, 6], [7, 9], [9, 4], [9, 6], [9, 7]]
result length = 12
๐ ์ฐ์ต๋ฌธ์ 1 (ํํ)
scores = ((3.7, 4.2), (2.9, 4.3), (4.1, 4.2))
sum = 0
for score1, score2 in scores:
sum += score1 + score2 #scores์ ์๋ ๋ชจ๋ ๊ฐ์ sum์ ๋ํ๋ค
avg = sum / (len(scores) * 2) #ํํ์ ๊ธธ์ด * 2 ๋งํผ ๋๋ ์ ํ๊ท ์ ๊ตฌํ๋ค
print('-'*40)
print(f'3ํ๋
์ด ํ์ : {sum}')
print(f'3ํ๋
ํ๊ท : {avg}')
print('-'*40)
grade4score = round((4.0 * 8 - sum), 1) #4ํ๋
๋ ํ์ํ ํ์ ์ ๊ตฌํ๋ค
minScore = grade4score / 2 #1, 2ํ๊ธฐ๋ก ๋๋ ์ผ ํ๊ธฐ ๋๋ฌธ์ 2๋ก ๋๋๋ค.
print(f'4ํ๋
๋ชฉํ ์ด ํ์ : {grade4score}')
print(f'4ํ๋
ํ ํ๊ธฐ ์ต์ ํ์ : {minScore}')
scores = list(scores) #list๋ก ๋ณํ
scores.append((minScore, minScore)) #list๋ก ๋ณํ๋ ์ํ์์ ์ต์ ํ์ append
scores = tuple(scores) #๋ค์ Tuple๋ก ๋ณํ
print(f'scores : {scores}')
>>>>
----------------------------------------
3ํ๋
์ด ํ์ : 23.4
3ํ๋
ํ๊ท : 3.9
----------------------------------------
4ํ๋
๋ชฉํ ์ด ํ์ : 8.6
4ํ๋
ํ ํ๊ธฐ ์ต์ ํ์ : 4.3
scores : ((3.7, 4.2), (2.9, 4.3), (4.1, 4.2), (4.3, 4.3))
๐ ์ฐ์ต๋ฌธ์ 2 (ํํ)
tuple1 = 1, 3, 2, 6, 12, 5, 7, 8
tuple2 = 0, 5, 2, 9, 8, 6, 17, 3
tempHap = list(tuple1)
tempGyo = list()
for n in tuple2:
if n not in tempHap:
tempHap.append(n)
else:
tempGyo.append(n)
tempHap = tuple(sorted(tempHap))
tempGyo = tuple(sorted(tempGyo))
print(f'ํฉ์งํฉ : {tempHap}')
print(f'๊ต์งํฉ : {tempGyo}')
>>>>
ํฉ์งํฉ : (0, 1, 2, 3, 5, 6, 7, 8, 9, 12, 17)
๊ต์งํฉ : (2, 3, 5, 6, 8)
๐ ์ฐ์ต๋ฌธ์ 3 (ํํ)
#์ํ ์ ์๋ฅผ ์
๋ ฅํ ํ ํํ์ ์ ์ฅํ๊ณ ๊ณผ๋ชฉ๋ณ ํ์ ์ ์ถ๋ ฅํด๋ณด์
korScore = int(input('kor : '))
engScore = int(input('eng : '))
matScore = int(input('mat : '))
sciScore = int(input('sci : '))
hisScore = int(input('his : '))
scores = ({'kor':korScore}, {'eng':engScore}, {'mat':matScore}, {'sci':sciScore}, {'his':hisScore})
print(f'scores : {scores}')
for item in scores: #scores์ item์ ๋ฝ์์จ๋ค ex) {'kor': 99}, ....
for key in item.keys(): #item.keys()๋ฅผ ํตํด key์ ๊ฐ ๊ฐ์ ํ ๋น es) kor, ....
if item[key] >= 90: #ex) item[kor] = ์ํ ์ ์, ์ซ์
item[key] = 'A'
elif item[key] >= 80:
item[key] = 'B'
elif item[key] >= 70:
item[key] = 'C'
elif item[key] >= 60:
item[key] = 'D'
else:
item[key] = 'F'
print(f'scores : {scores}')
>>>>
kor : 99
eng : 55
mat : 67
sci : 80
his : 75
scores : ({'kor': 99}, {'eng': 55}, {'mat': 67}, {'sci': 80}, {'his': 75})
scores : ({'kor': 'A'}, {'eng': 'F'}, {'mat': 'D'}, {'sci': 'B'}, {'his': 'C'})
๐ ์ฐ์ต๋ฌธ์ 4 (ํํ)
#์ค๋ฆ์ฐจ์ ์ ๋ ฌ (ํ๋ฒ ๋ ๋ณต์ต!)
fruits = ({'์๋ฐ':8}, {'ํฌ๋':13}, {'์ฐธ์ธ':12}, {'์ฌ๊ณผ':17}, {'์๋':19}, {'์๋ชฝ':15})
print(fruits)
fruits = list(fruits)
currentIdx = 0; nextIdx = 1
eIdx = len(fruits) - 1
flag = True
while flag:
curDic = fruits[currentIdx]
nextDic = fruits[nextIdx]
curDicCnt = list(curDic.values())[0]
nextDicCnt = list(nextDic.values())[0]
if nextDicCnt < curDicCnt:
fruits.insert(currentIdx, fruits.pop(nextIdx))
nextIdx = currentIdx+1
continue
nextIdx += 1
if nextIdx > eIdx:
currentIdx += 1
nextIdx = currentIdx + 1
if currentIdx == 5:
flag = False
print(tuple(fruits))
>>>>
({'์๋ฐ': 8}, {'ํฌ๋': 13}, {'์ฐธ์ธ': 12}, {'์ฌ๊ณผ': 17}, {'์๋': 19}, {'์๋ชฝ': 15})
({'์๋ฐ': 8}, {'์ฐธ์ธ': 12}, {'ํฌ๋': 13}, {'์๋ชฝ': 15}, {'์ฌ๊ณผ': 17}, {'์๋': 19})
๐ ์ฐ์ต๋ฌธ์ 5 (ํํ)
studentCnt = ({'cls01' : 18},
{'cls02' : 21},
{'cls03' : 20},
{'cls04' : 19},
{'cls05' : 22},
{'cls06' : 20},
{'cls07' : 23},
{'cls08' : 17},
)
totalCnt = 0
minStdCnt = 0; minCls = ''
maxStdCnt = 0; maxCls = ''
deviation = []
for idx, dic in enumerate(studentCnt):
for k, v in dic.items():
totalCnt += v
avg = totalCnt / len(studentCnt)
if minStdCnt == 0 or minStdCnt > v:
minStdCnt = v
minCls = k
if maxStdCnt < v:
maxStdCnt = v
maxCls = k
print('์ ์ฒด ํ์ ์ : {}๋ช
'.format(totalCnt))
print('ํ๊ท ํ์ ์ : {}๋ช
'.format(int(avg)))
print('ํ์ ์๊ฐ ๊ฐ์ฅ ์ ์ ํ๊ธ : {}ํ๊ธ, ({}๋ช
)'.format(minCls, minStdCnt))
print('ํ์ ์๊ฐ ๊ฐ์ฅ ๋ง์ ํ๊ธ : {}ํ๊ธ, ({}๋ช
)'.format(maxCls, maxStdCnt))
for idx, dic in enumerate(studentCnt):
for k, v in dic.items():
deviation.append({k : v - avg})
print('ํ๊ธ๋ณ ํ์ ํธ์ : {}'.format(deviation))
>>>>
์ ์ฒด ํ์ ์ : 160๋ช
ํ๊ท ํ์ ์ : 20๋ช
ํ์ ์๊ฐ ๊ฐ์ฅ ์ ์ ํ๊ธ : cls08ํ๊ธ, (17๋ช
)
ํ์ ์๊ฐ ๊ฐ์ฅ ๋ง์ ํ๊ธ : cls07ํ๊ธ, (23๋ช
)
ํ๊ธ๋ณ ํ์ ํธ์ : [{'cls01': -2.0}, {'cls02': 1.0}, {'cls03': 0.0}, {'cls04': -1.0}, {'cls05': 2.0}, {'cls06': 0.0}, {'cls07': 3.0}, {'cls08': -3.0}]
๐ ์ฐ์ต๋ฌธ์ 1 (๋์ ๋๋ฆฌ)
#1~10๊น์ง ๊ฐ ์ ์์ ๋ํ ์ฝ์๋ฅผ ์ ์ฅํ๋ ๋์
๋๋ฆฌ๋ฅผ ๋ง๋ค๊ณ , ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์
dic = {}
for n1 in range(2, 11):
tempList = []
for n2 in range(1, n1+1):
if n1 % n2 == 0:
tempList.append(n2)
dic[n1] = tempList
print(dic)
>>>>
{2: [1, 2], 3: [1, 3], 4: [1, 2, 4], 5: [1, 5], 6: [1, 2, 3, 6], 7: [1, 7], 8: [1, 2, 4, 8], 9: [1, 3, 9], 10: [1, 2, 5, 10]}
๐ ์ฐ์ต๋ฌธ์ 2 (๋์ ๋๋ฆฌ)
members = {}
n = 1
while n < 6:
mail = input('๋ฉ์ผ์
๋ ฅ : ')
pw = input('๋น๋ฒ์
๋ ฅ : ')
if mail in members:
print('์ด๋ฏธ ์ฌ์ฉ์ค์ธ ๋ฉ์ผ ๊ณ์ ์
๋๋ค.')
continue
else:
members[mail] = pw
n += 1
for key in members.keys():
print(f'{key} : {members[key]}')
while True:
delMail = input('์ญ์ ํ ๊ณ์ (๋ฉ์ผ) ์
๋ ฅ : ')
if delMail in members:
delPw = input('๋น๋ฒ ์
๋ ฅ: ')
if members[delMail] == delPw:
del members[delMail]
print(f'{delMail} ๊ณ์ ์ญ์ ์๋ฃ!')
break
else:
print(f'{delMail}์ ๋น๋ฒ ํ์ธ ์๋ง')
else:
print('๊ณ์ ํ์ธ ์๋ง!')
for key in members.keys():
print(f'{key} : {members[key]}')
๐ ์ฐ์ต๋ฌธ์ 3 (๋์ ๋๋ฆฌ)
students = {'S21-0001':{'์ด๋ฆ':'์ต์ฑํ',
'์ฑ๊ตฌ๋ถ':'M',
'์ ๊ณต':'๋์์ธ',
'์ฐ๋ฝ์ฒ':'010-1234-5678',
'๋ฉ์ผ':'hun@gmail.com',
'์ทจ๋ฏธ':['๋๊ตฌ, ์์
']},
'S21-0002':{'์ด๋ฆ':'ํ์์ฐ',
'์ฑ๊ตฌ๋ถ':'M',
'์ ๊ณต':'๋ฐ๋ฆฌ์คํธ',
'์ฐ๋ฝ์ฒ':'010-5678-9012',
'๋ฉ์ผ':'yeong@gmail.com',
'์ทจ๋ฏธ':['์ถ๊ตฌ']},
'S21-0003':{'์ด๋ฆ':'ํฉ์ง์',
'์ฑ๊ตฌ๋ถ':'W',
'์ ๊ณต':'์์
',
'์ฐ๋ฝ์ฒ':'010-9012-3456',
'๋ฉ์ผ':'jin@gmail.com',
'์ทจ๋ฏธ':['์์, ์ฝ๋ฉ']}
}
for k1 in students.keys():
print('-'*40)
print('ํ์๋ฒํธ : {}'.format(k1))
student = students[k1]
for k2 in student.keys():
print('{} : {}'.format(k2, student[k2]))
memNo = input('์กฐํํ ํ์๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์.: ')
print('{} : {}'.format(memNo, students[memNo]))
>>>>
----------------------------------------
ํ์๋ฒํธ : S21-0001
์ด๋ฆ : ์ต์ฑํ
์ฑ๊ตฌ๋ถ : M
์ ๊ณต : ๋์์ธ
์ฐ๋ฝ์ฒ : 010-1234-5678
๋ฉ์ผ : hun@gmail.com
์ทจ๋ฏธ : ['๋๊ตฌ, ์์
']
----------------------------------------
ํ์๋ฒํธ : S21-0002
์ด๋ฆ : ํ์์ฐ
์ฑ๊ตฌ๋ถ : M
์ ๊ณต : ๋ฐ๋ฆฌ์คํธ
์ฐ๋ฝ์ฒ : 010-5678-9012
๋ฉ์ผ : yeong@gmail.com
์ทจ๋ฏธ : ['์ถ๊ตฌ']
----------------------------------------
ํ์๋ฒํธ : S21-0003
์ด๋ฆ : ํฉ์ง์
์ฑ๊ตฌ๋ถ : W
์ ๊ณต : ์์
์ฐ๋ฝ์ฒ : 010-9012-3456
๋ฉ์ผ : jin@gmail.com
์ทจ๋ฏธ : ['์์, ์ฝ๋ฉ']
์กฐํํ ํ์๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์.: S21-0003
S21-0003 : {'์ด๋ฆ': 'ํฉ์ง์', '์ฑ๊ตฌ๋ถ': 'W', '์ ๊ณต': '์์
', '์ฐ๋ฝ์ฒ': '010-9012-3456', '๋ฉ์ผ': 'jin@gmail.com', '์ทจ๋ฏธ': ['์์, ์ฝ๋ฉ']}
'Study_note(zb_data) > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํฐ๋ ๋ ธํธ (์๊ณ ๋ฆฌ์ฆ 19-30)_0725 (0) | 2023.07.31 |
---|---|
์คํฐ๋ ๋ ธํธ (์๊ณ ๋ฆฌ์ฆ 1-18)_0724 (0) | 2023.07.31 |
์คํฐ๋ ๋ ธํธ (์๋ฃ๊ตฌ์กฐ 27~38)_0720 (0) | 2023.07.24 |
์คํฐ๋ ๋ ธํธ (์๋ฃ๊ตฌ์กฐ 01~26)_0719 (0) | 2023.07.24 |
์คํฐ๋ ๋ ธํธ (๊ธฐ์ด ์ํ ๋ฌธ์ ํ์ด 31 ~ 45)_0718 (0) | 2023.07.20 |