โ๐ป ๋ฐฐ์ด์
ํ์ด์ฌ์ผ๋ก ๊ฐ๋จํ ์์ ๋ค์ ํตํด ์กฐ๊ฑด๋ฌธ์ ๊ณต๋ถํ ์ ์์๋ค.
๋ํ, ํ์ด์ฌ์ ์ฌ์ฉํ์ฌ ํ์ผ์ ์ฝ๊ณ ์ฐ๋ฉฐ ํด๋ ์์ ์ ๋ํด์๋ ๊ณต๋ถํ์ฌ ๋ฐ์ดํฐ๊ฐ ๋ง์ ์์ ์์๋ ์ฝ๊ฒ ์์ ํ ์ ์์๋ค.
์ค์ ๋ฐ์ดํฐ๋ค์ ํตํด ์์ ๋ฐฐ์ด ๊ฒ๋ค์ ์ ์ฉํด๋ณด๊ณ ๋ ์์๊ฐ ์ ์์๋ค.
(ํนํ, ๋งฅ ํ๊ฒฝ์์ ์ฃผ์ํ ์ ๋ ํ์ ํ ์ ์์ด ๋ค์์๋ ๊ฐ์ ์ค์๋ฅผ ํ์ง ์์ ์ ์์ ๊ฒ ๊ฐ๋ค :D)
๐ ๋ชฉ์ฐจ
1. ์กฐ๊ฑด๋ฌธ ์ฐ์ต 1 : ์ฌ์ฉ์ ์ ๋ ฅ์ ๋ฐ๋ฅธ ๋ค๋ฅธ ๊ฒฐ๊ณผ ์ถ๋ ฅ
2. ์กฐ๊ฑด๋ฌธ ์ฐ์ต 2 : ์ค๋ ํ์ธ
3. list comprehension (๋ฆฌ์คํธ ์ถ์ฝํ)
4. multiLine
5. ๋ ธ๋ ๊ฐ์ฌ์์ ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ ๋จ์ด ์กฐํํ๊ธฐ
6. ํ์ฌ ์์ ๊ฒฝ๋ก ํ์ธํ๊ธฐ
7. ์ ๋ ๊ฒฝ๋ก์ ์๋ ๊ฒฝ๋ก
8. ์์ฃผ ์ฌ์ฉํ๋ ํด๋ ์์ ๋ช ๋ น์ด
9. ํ์ผ ์ฐ๊ธฐ ๋ฐ ์ฝ๊ธฐ
10. ํ์ผ ์ฎ๊ธฐ๊ธฐ
11. ์ฑ๋ณ์ ๋ฐ๋ฅธ ํ์ผ์ ๋ค๋ฅธ ํด๋๋ก ๊ตฌ๋ถ ํ ์ฑ๋ณ์ ๋ฐ๋ฅธ ํ๊ท ๊ฐ ๊ตฌํ๊ธฐ
12. ์คํ๋ฒ ์ค ๋งค์ฅ ์ฌ์ง์ ๋ค๋ฅธ ํด๋์ ์ ์ฅํ๊ณ ํด๋น ์ด๋ฏธ์ง ๋ช ์ ๋งค์ฅ๋ช ์ผ๋ก ์ ์ฅํ๊ธฐ
๋ค์ด๊ฐ๋ฉฐ
์ง๋ ํ์ด์ฌ ๊ธฐ์ด 1์ ๋ฆฌ๋ทฐํ ํ, ์ง์ ์์ ์ฝ๋๋ฅผ ๊ตฌํํ ํ ์ ๋ต๊ณผ ๋น๊ตํด๋ณด์๋ค.
๋ถ์กฑํ ๋ถ๋ถ์ ์ง์คํ๊ณ ์ ๊ฒฝ์จ์ผ ํ ๋ถ๋ถ๋ค์ ๋ํด ์ง๊ณ ๋์ด๊ฐ๊ณ ์ ํ๋ค.
์กฐ๊ฑด๋ฌธ ์ฐ์ต 1 : ์ฌ์ฉ์ ์ ๋ ฅ์ ๋ฐ๋ฅธ ๋ค๋ฅธ ๊ฒฐ๊ณผ ์ถ๋ ฅ
# ์กฐ๊ฑด๋ฌธ ์ฐ์ต
# distance ๋ผ๋ ๋ณ์์๋ ์์ ๊ฐ์ด ๋ค์ด๊ฐ ์์ ์
๋๋ค.
# ์ด distance ๊ฐ 3km ์ดํ๋ก ์๋ค๋ฉด "๊ฑธ์ด ๊ฐ์ธ์" ๋ผ๊ณ ๋งํ๊ณ
# 3km ๋ณด๋ค ๋ฉ๊ณ 10 km ๋ณด๋ค ์๋ค๋ฉด "๋ฒ์ค ํ์ธ์" ๋ผ๊ณ ๋งํด์ผ ํฉ๋๋ค
# ๋ง์ฝ 10km ์ด์์ด๋ผ๋ฉด "ํ์ ํ์ธ์"๋ผ๊ณ ๋งํด์ผ ํฉ๋๋ค
# distance ์ ์๊ด์์ด "์๊ณ ํ์
จ์ต๋๋ค" ๋ฅผ ์ถ๋ ฅํฉ๋๋ค
distance = input("input distance?")
distance = int(distance) # ๋ฌธ์์ด๋ก ์
๋ ฅ๋ฐ๊ธฐ ๋๋ฌธ์ int๋ก ๋ณํ์ ํด์ฃผ์ด์ผ ํจ
if distance <= 3:
print("๊ฑธ์ด ๊ฐ์ธ์")
elif distance < 10:
print("๋ฒ์ค ํ์ธ์")
else:
print("ํ์ ํ์ธ์")
print("์๊ณ ํ์
จ์ต๋๋ค")
input ๋ฉ์๋๋ฅผ ํตํด ์ฌ์ฉ์์๊ฒ ์ ๋ ฅ์ ๋ฐ๋๋ค.
์ด ๋, input์ string ํ์ด๊ธฐ ๋๋ฌธ์ distance ์ ๊ฐ์ int ํ๊ณผ ๋น๊ต ํ๊ธฐ ์ํด์ int ๋ก ํ ๋ณํ์ ํด์ฃผ์ด์ผ ํ๋ค.
์กฐ๊ฑด๋ฌธ ์ฐ์ต 2 : ์ค๋ ํ์ธ
# ์ค๋
ํ์ธ
for year in range(1900, 2101):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(f"{year} : ์ค๋
")
else:
print(f"{year} : ํ๋
")
else:
print(f"{year} : ์ค๋
")
depth ๊ฐ 3์ผ๋ก, ํด๋ฆฐ ์ฝ๋ ์ธก๋ฉด์์ ๋นํจ์จ์ ์ธ ๋์ ์ฝ๋์ด๋ค.
for year in range(1900, 2101):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
print(f"{year} : ์ค๋
")
์ด๋ ๊ฒ ํ ์ค๋ก ๊น๋ํ๊ฒ ์ฝ๋ฉํ๋ ๋ฒ์ ์ตํ์ผ ํ๋ค.
list comprehension (๋ฆฌ์คํธ ์ถ์ฝํ)
total = [x for x in range(1, 5)]
# 15 ๋ฐฐ์ ๊ตฌํ๊ธฐ ์์
total2 = [x for x in range(1, 100) if x % 3 == 0 and x % 5 == 0]
๋ณต์กํ ์ฝ๋๋ณด๋ค ์ด๋ ๊ฒ ๊ฐ๋จํ๊ฒ ํ ์ค๋ก๋ ์์ฑํ ์ ์๋ค.
multiLine
multiline = """dddd
ddddd"""
multiline์ """ ์ผ๋ก ์ด๋ฃจ์ด์ง๋ฉฐ ๊ฐํ ๋ฌธ์์ธ '\n' ๋ ์ ์ฅ์ด ๋๋ค.
'
๋ ธ๋ ๊ฐ์ฌ์์ ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ ๋จ์ด ์กฐํํ๊ธฐ
ditto = '''Woo woo woo woo ooh
Woo woo woo woo
Stay in the middle
Like you a little
Don’t want no riddle
๋งํด์ค say it back
Oh say it ditto
์์นจ์ ๋๋ฌด ๋ฉ์ด
So say it ditto
ํ์ฉ ์ปค๋ฒ๋ ธ์ด
ํจ๊ปํ ๊ธฐ์ต์ฒ๋ผ
๋ ๋ณด๋ ๋ด ๋ง์์
์ด๋์ ์ฌ๋ฆ ์ง๋ ๊ฐ์
๊ธฐ๋ค๋ ธ์ง all this time
Do you want somebody
Like I want somebody
๋ ๋ณด๊ณ ์์์ง๋ง
Do you think about me now yeah
All the time yeah
All the time
I got no time to lose
๋ด ๊ธธ์๋ ํ๋ฃจ
๋ ๋ณด๊ณ ์ถ์ด
Ra-ta-ta-ta ์ธ๋ฆฐ ์ฌ์ฅ (Ra-ta-ta-ta)
I got nothing to lose
๋ ์ข์ํ๋ค๊ณ
wooah wooah wooah
Ra-ta-ta-ta ์ธ๋ฆฐ ์ฌ์ฅ (Ra-ta-ta-ta)
But I don't want to
Stay in the middle
Like you a little
Don’t want no riddle
๋งํด์ค say it back
Oh say it ditto
์์นจ์ ๋๋ฌด ๋ฉ์ด
So say it ditto
I don't want to
Walk in this ๋ฏธ๋ก
๋ค ์๋ ๊ฑด ์๋์ด๋
๋ฐ๋ผ๋ ๋๋ก
๋งํด์ค Say it back
Oh say it ditto
I want you so, want you
So say it ditto
Not just anybody
๋๋ฅผ ์์ํ์ง
ํญ์ ๋ฟ์์๋
์ฒ์ ๋๋ ๊ทธ๋๋ก ๋
๊ธฐ๋ค๋ ธ์ง all this time
I got nothing to lose
๋ ์ข์ํ๋ค๊ณ
wooah wooah wooah
Ra-ta-ta-ta ์ธ๋ฆฐ ์ฌ์ฅ (Ra-ta-ta-ta)
But I don't want to
Stay in the middle
Like you a little
Don’t want no riddle
๋งํด์ค say it back
Oh say it ditto
์์นจ์ ๋๋ฌด ๋ฉ์ด
So say it ditto
I don't want to
Walk in this ๋ฏธ๋ก
๋ค ์๋ ๊ฑด ์๋์ด๋
๋ฐ๋ผ๋ ๋๋ก
๋งํด์ค Say it back
Oh say it ditto
I want you so, want you
So say it ditto
Woo woo woo woo ooh
Woo woo woo woo'''
ditto ๋ผ๋ ๋ ธ๋์ ๊ฐ์ฌ์ด๋ค.
ditto = ditto.lower()
ditto ์ ๊ฐ์ฌ์๋ ๋์๋ฌธ์๊ฐ ์์ฌ ์๊ธฐ ๋๋ฌธ์ lower ๋ฉ์๋๋ฅผ ํตํด ๋ชจ๋ ์๋ฌธ์๋ก ๋ฐ๊พธ์ด ์ค๋ค.
dict_test = {}
temp = ditto.split();
for word in temp:
if word in dict_test:
dict_test[word] += 1
else:
dict_test[word] = 1
๊ทธ ์ ๊ธ์์ ๊ณต๋ถํ๋ ๋๋ก, ๋น dictionary ๋ฅผ ๋ง๋ค๊ณ split ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ๋จ์ด๋ฅผ ๊ตฌ๋ถํ๋ค.
๊ทธ ํ, ์ด๋ฏธ dict_test ์์ word ๊ฐ ์๋ค๋ฉด ๊ฐ์๋ฅผ + 1, ์๋ค๋ฉด 1๋ก ๋ง๋ค์ด์ค๋ค.
def tmp(x):
return x[1]
sorted(dict_test.items(), key = tmp, reverse = True) # key ๋ถ๋ถ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํด์ค๋ค.
key ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌํ๋ฉด, ๊ฐ์ฌ์ ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ ์์ผ๋ก ๊ฐ ๋จ์ด์ ๋ฑ์ฅ ํ์๋ฅผ ์ ์ ์๋ค.
ํ์ฌ ์์ ๊ฒฝ๋ก ํ์ธํ๊ธฐ
import os
os.getcwd() # ๋ด๊ฐ ์ง๊ธ ์์
ํ๋ ๊ฒฝ๋ก
๋ด๊ฐ ์ง๊ธ ์์ ํ๋ ํ์ฌ ๊ฒฝ๋ก๊ฐ ์ด๋์ธ์ง ์ ์ ์๋ค.
์ ๋ ๊ฒฝ๋ก์ ์๋ ๊ฒฝ๋ก
์ ๋ ๊ฒฝ๋ก๋, ์ ์ฒด ๊ฒฝ๋ก๋ฅผ ์๋ฏธํ๊ณ , (์: c :\\workspace\\test)
์๋ ๊ฒฝ๋ก๋, ํ์ฌ ์์น '๊ธฐ์ค'์ผ๋ก์ ๊ฒฝ๋ก๋ฅผ ์๋ฏธํ๋ค. (์: .\test)
์์ฃผ ์ฌ์ฉํ๋ ํด๋ ์์
๋ช
๋ น์ด
os.mkdir("") # make directory
os.chdir("") # ๋ค๋ฅธ directory ๋ก change
os.listdir("") # ํด๋น directory ์์ ์๋ ํ์ผ์ list
์ ๋ช ๋ น์ด๋ค์ ์์๋๋ฉด ์ ์ฉํ๋ค.
ํ์ผ ์ฐ๊ธฐ ๋ฐ ์ฝ๊ธฐ
f = open("./pic.txt", "w", encoding='utf-8')
for roots, dirs, files in os.walk("c:/Windows/"):
for file in files:
f.write(f"{roots}/{file}\n")
f.close()
ํ์ผ์ ์ฐ๊ณ ("w")
f = open('./์ด๋ฆ_์๋
_์ฑ๋ณ_10000/', "r", encoding='utf-8')
f.close()
ํ์ผ์ ์ฝ์("r") ์ ์๋ค.
ํ์ผ ์ฎ๊ธฐ๊ธฐ
import shutil
shutil.move("./pic.txt", "./test/") # pic.txt ํ์ผ์ test ํด๋๋ก ์ฎ๊ธฐ๊ธฐ
shutil ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ move ๋ฉ์๋๋ฅผ ํตํด ๋ค๋ฅธ ๊ณณ์ ์๋ ํ์ผ์ ๋ ๋ค๋ฅธ ํด๋ ์์ผ๋ก ์์น๋ฅผ ๋ฐ๊ฟ ์ ์๋ค.
์ฑ๋ณ์ ๋ฐ๋ฅธ ํ์ผ์ ๋ค๋ฅธ ํด๋๋ก ๊ตฌ๋ถ ํ ์ฑ๋ณ์ ๋ฐ๋ฅธ ํ๊ท ๊ฐ ๊ตฌํ๊ธฐ
### ๋ฌธ์ ์๊ตฌ์ฌํญ
# male ํด๋ ์์ 1, 3 ํ์ผ ์ฎ๊ธฐ๊ธฐ
# female ํด๋ ์์ 2, 4 ํ์ผ ์ฎ๊ธฐ๊ธฐ
# ํ์ผ์ ์ฝ์ด์ male ๋ฆฌ์คํธ, female ๋ฆฌ์คํธ์ ๊ฐ๊ฐ append ํ๊ธฐ
# ํ๊ท ๊ตฌํ๊ธฐ
import shutil
import os
if os.path.isdir("./male") == False:
os.mkdir("./male")
if os.path.isdir("./female") == False:
os.mkdir("./female")
for file in os.listdir("./์ด๋ฆ_์๋
_์ฑ๋ณ_10000/"):
if file.split(".")[-1] == 'txt':
if file.split("_")[2][0] == '1' or file.split("_")[2][0] == '3':
shutil.move(f"./์ด๋ฆ_์๋
_์ฑ๋ณ_10000/{file}", "./male/")
else:
shutil.move(f"./์ด๋ฆ_์๋
_์ฑ๋ณ_10000/{file}", "./female/")
male = []
for file in os.listdir("./male/"):
if file.split(".")[-1] == 'txt':
f = open(f"./male/{file}", "r", encoding='utf-8')
txt = f.read()
txt = int(txt.replace(",", ""))
male.append(txt)
f.close()
female = []
for file in os.listdir("./female/"):
if file.split(".")[-1] == 'txt':
f = open(f"./female/{file}", "r", encoding='utf-8')
txt = f.read()
txt = int(txt.replace(",", ""))
female.append(txt)
f.close()
male_avg = sum(male) / len(male)
female_avg = sum(female) / len(female)
male_avg
female_avg
๊ตฌํ ์์ฒด๋ ์ด๋ ต์ง ์์ ๋ฌธ์ ์๋ค.
๊ทธ๋ฌ๋, ๋งฅ ํ๊ฒฝ์ ์๋กญ๊ฒ ์์ฑ๋๋ ํ์ผ๋ค์ด ์๊ธฐ ๋๋ฌธ์, ์๋ฌ๊ฐ ๋๋ค๋ฉด ์ด๋ฅผ ์กฐ๊ฑด๋ฌธ์ ํตํด ๊ฑธ๋ฌ์ฃผ๋ ๋ก์ง์ ํญ์ ์ถ๊ฐํด์ผ ํ๋ค๊ณ ํ๋ค.
(๋งฅ์์๋ '.DS_Store' ๋ผ๋ ํ์ผ์ ์์ฑํ๋๋ฐ, ์ ์ฒด ํ์ผ์ ๋ณด๋ค๋ณด๋ฉด ์ด๋ฅผ ๊ฑธ๋ฌ์ฃผ๋ ๋ก์ง์ด ์์ด ์๋ฌ๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ค๋ถํ๋ ์ฃผ์ํด์ผ ํ๋ค.)
์กฐ๊ฑด๋ฌธ์ ํตํด ํ์ผ ํ์ฅ์๋ช ์ ์๋์ ๊ฐ์ด ์กฐ๊ฑด์ ๊ผญ ํฌํจ์ํค๋๋ก ํ์.
if file.split(".")[-1] == 'txt':
๋ด๊ฐ ์ดํผ๊ณ ์ถ์ ํ์ผ ํ์ฅ์๋ช ์ ๋ฐ๋ก ์ง์ ํด์ฃผ๋ ์กฐ๊ฑด๋ฌธ์ ํตํด ์ํํ ์งํ์ด ๊ฐ๋ฅํ๋ค.
์คํ๋ฒ ์ค ๋งค์ฅ ์ฌ์ง์ ๋ค๋ฅธ ํด๋์ ์ ์ฅํ๊ณ ํด๋น ์ด๋ฏธ์ง ๋ช ์ ๋งค์ฅ๋ช ์ผ๋ก ์ ์ฅํ๊ธฐ
import requests
url = "https://www.starbucks.co.kr/store/getStore.do?r=GKYHQKU7D3"
payload = {"in_biz_cds" : "0",
"in_scodes" : "0",
"ins_lat" : "37.566535",
"ins_lng" : "126.9779692",
"search_text" : "",
"p_sido_cd" : "01",
"p_gugun_cd" : "",
"in_distance" : "0",
"in_biz_cd" : "",
"isError" : "true",
"searchType" : "C",
"set_date" : "",
"all_store" : "0",
"T03" : "0",
"T01" : "0",
"T27" : "0",
"T12" : "0",
"T09" : "0",
"T30" : "0",
"T05" : "0",
"T22" : "0",
"T21" : "0",
"T10" : "0",
"T36" : "0",
"T43" : "0",
"T48" : "0",
"P10" : "0",
"P50" : "0",
"P20" : "0",
"P60" : "0",
"P30" : "0",
"P70" : "0",
"P40" : "0",
"P80" : "0",
"whcroad_yn" : "0",
"P90" : "0",
"new_bool" : "0",
"iend" : "1000",
"rndCod" : "IZVHHSIFWC",}
r = requests.post(url, data=payload)
star = r.json()['list']
์ด๋ฒ์๋ ์์ธ์ ์คํ๋ฒ ์ค ๋งค์ฅ์ ์นํ์ด์ง์์ ํฌ๋กค๋ง์ ํตํด ์งํํ๋ค.
# ์คํ๋ฒ
์ค ๋งค์ฅ ์ฌ์ง์ starbuck ํด๋ ์์ฑํ๊ณ
# ๋งค์ฅ๋ช
.jpg ํ์ผ์ ์ ์ฅ
from urllib import request
import shutil
import os
if os.path.isdir("./starbucks") == False:
os.mkdir("./starbucks")
for x in star:
if x['defaultimage'] != None:
if x['defaultimage'].split(".")[-1] == 'jpg':
request.urlretrieve("https://www.starbucks.co.kr" + x['defaultimage'], f"./starbucks/{x['s_name']}.jpg")
elif x['defaultimage'].split(".")[-1] == 'png':
request.urlretrieve("https://www.starbucks.co.kr" + x['defaultimage'], f"./starbucks/{x['s_name']}.png")
elif x['defaultimage'].split(".")[-1] == 'JPG':
request.urlretrieve("https://www.starbucks.co.kr" + x['defaultimage'], f"./starbucks/{x['s_name']}.JPG")
์คํ๋ฒ ์ค ๋งค์ฅ ์ฌ์ง์ ๊ทธ๋ฅ ๋ค์ด๋ก๋ ํ๊ฒ ๋ ๊ฒฝ์ฐ,
์ง๊ธ ํ์ฌ ์์ ํ๋ ๋๋ ํ ๋ฆฌ์ ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ ์ด๋ฏธ์ง๋ฅผ ์ ์ฅํ ์๋ก์ด ํด๋๋ฅผ ๋ง๋ค์ด ์ฃผ์๋ค.
๊ทธ๋ฆฌ๊ณ , ๊ทธ๋ฅ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด ๋ฐ์ผ๋ฉด ์ด๋ฏธ์ง์ ์ด๋ฆ์ด ์ซ์๋ง์ผ๋ก ๊ตฌ์ฑ๋์ด ์ด๋ค ๋งค์ฅ์ธ์ง ์ฝ๊ฒ ์ดํดํ ์ ์๋ค.
๋ฐ๋ผ์, ์คํ๋ฒ ์ค ๋งค์ฅ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด ๋ฐ์ ๋, ๋งค์ฅ๋ช ์ด ๋ค์ด๊ฐ๋๋ก ์ด๋ฏธ์ง ํ์ผ์ ์ ์ฅํ๋๋ก ํ๋ค.
'Python > [๊ธฐ์ด ๊ฐ์ ์ ๋ฆฌ]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| python ๊ธฐ์ด 6 (0) | 2023.02.26 |
|---|---|
| python ๊ธฐ์ด 5 (0) | 2023.02.26 |
| python ๊ธฐ์ด 4 (1) | 2023.02.25 |
| python ๊ธฐ์ด 3 (0) | 2023.02.24 |
| Python ๊ธฐ์ด 1 (0) | 2023.02.13 |