MapleStory Finger Point

๐Ÿ’Ž Python/Python ๊ฐœ๋…์ •๋ฆฌ

๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ๋”•์…”๋„ˆ๋ฆฌ

HYEJU01 2024. 4. 8. 00:54

โ–ถ ๋ฆฌ์ŠคํŠธ ๊ฐœ๋… (ํŒŒ์ด์ฌ์˜ ๋ฆฌ์ŠคํŠธ๋Š” ๋ฐฐ์—ด๊ณผ ๋‹ค๋ฅด๋‹ค.)

  1. ํƒ€์ž…์— ๊ด€๊ณ„๊ฐ€ ์—†์Œ
    • ๋ฐฐ์—ด์€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํ˜•๋งŒ ์ €์žฅ๊ฐ€๋Šฅ
    • ๋ฆฌ์ŠคํŠธ๋Š” ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํ˜• ์ €์žฅ๊ฐ€๋Šฅ. (์ž๋™ ํ˜• ์ €์žฅ)
  2. ์ฒจ์ž(์ธ๋ฑ์Šค) ์‚ฌ์šฉ ๊ฐ€๋Šฅ
aa = [0,0,0] //๋ฆฌ์ŠคํŠธ ์ดˆ๊ธฐํ™” ๊ฐ€๋Šฅ
 
aa[0], aa[1], aa[2]  

3.
  1. ๊ฐ์ฒด
  • ๋ณ€์ˆ˜์˜ ๊ธฐ๋Šฅ๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ
  • ๊ฐ์ฒด = ์†์„ฑ(๋ณ€์ˆ˜)+ํ–‰์œ„(ํ•จ์ˆ˜)

โ–ถ ๋ฆฌ์ŠคํŠธ ๊ฐ’ ์ถ”๊ฐ€ / append()

aa = [] //๋ฆฌ์ŠคํŠธ ํ•จ์ˆ˜๋ผ๊ณ  ์„ ์–ธ๋งŒ ํ•ด์คŒ
aa.append(0)
aa.append(1)
print(a)

โ–ถ ๋ฆฌ์ŠคํŠธ ์‚ฌ์šฉ

aa = []
for i in range(0,4)
	aa.append(0)
len(aa)

#1) for ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ
#2) len()  : ๋ฆฌ์ŠคํŠธ ๊ฐœ์ˆ˜
#3) str() : ์ˆซ์ž๋ฅผ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜
aa = [1,2,3,4]
bb = [5,6,7,8]

# ์Œ์ˆ˜ ๊ฐ’ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
aa[-1] #4

# : ๋กœ ๋ฆฌ์ŠคํŠธ ๋ฒ”์œ„ ์ง€์ •
# [์ฃผ์˜!!] ๋ฆฌ์ŠคํŠธ๋ช…[์‹œ์ž‘๊ฐ’:๋๊ฐ’+1]
aa[0:3]      #0~2 ์ธ๋ฑ์Šค => 123

# : ์ฝœ๋ก  ์•ž ๋’ค ์ˆซ์ž ์ƒ๋žตํ•ด์„œ ์ง€์ •
aa[2:]      #2~๋ ์ธ๋ฑ์Šค => 34
aa[:2]      #0~1 ์ธ๋ฑ์Šค [์ฃผ์˜!!] 2๋Š” ํฌํ•จ์•ˆ๋จ!!

# ๋ฆฌ์ŠคํŠธ ๋ผ๋ฆฌ ๋ง์…ˆ
aa + bb     #12345678 

# ๋ฆฌ์ŠคํŠธ ๊ณฑ์…ˆ
aa * 3     #123412341234 ๋ฐ˜๋ณตํ•จ

# ๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ ๊ฑด๋„ˆ๋›ฐ๋ฉด์„œ ์ถ”์ถœ
aa[::2]     #2์”ฉ ๊ฑด๋„ˆ๋œ€
aa[::-1]     #๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅ

โ–ถ ๋ฆฌ์ŠคํŠธ ๋ณ€๊ฒฝ

  1. ์œ„์น˜ ๊ฐ’ ์ง€์ •ํ•ด์„œ ๊ฐ’ ๋ณ€๊ฒฝ
  2. aa = [10,20,30] aa[1] = 100 aa # [10,200,30]
  3. : ์ฝœ๋ก  ์ด์šฉํ•ด์„œ ๋ฒ”์œ„ ์ง€์ • ํ›„ ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ
aa = [10,20,30]
aa[1:2] = [200,201]
aa

#[10,200,201,30]

โ–ถ ๋ฆฌ์ŠคํŠธ ์‚ญ์ œ

aa = [1,2,3,4]

aa=[]          # 1) ๋นˆ ๋ฆฌ์ŠคํŠธ
aa = None      # 2) ๋นˆ ๋ณ€์ˆ˜
del(aa)        # 3) ๋ณ€์ˆ˜ ์‚ญ์ œ

โ–ถ ๋ฆฌ์ŠคํŠธ ์กฐ์ž‘ ํ•จ์ˆ˜

๋ฆฌ์ŠคํŠธ๋ช….append()
๋ฆฌ์ŠคํŠธ๋ช….pop()
๋ฆฌ์ŠคํŠธ๋ช….sort()
๋“ฑ๋“ฑ
  • ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ ๋ณ€๊ฒฝ ํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
  • myList = [1,2,3] newList = sorted(myList)

โ–ถ2์ฐจ์› ๋ฆฌ์ŠคํŠธ ๊ฐœ๋…

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

bb = [[1,2],[3,4,5],[6,7,8,9]] // ๋ถˆ๊ทœ์น™ํ•œ ํฌ๊ธฐ ๊ฐ€๋Šฅ
  • 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋Š” ์ค‘์ฒฉ for ๋ฌธ ์ด์šฉ
  • ๋ถˆ๊ทœ์น™ํ•œ ํฌ๊ธฐ๋กœ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ ๊ฐ€๋Šฅ

[Self Study 7-1]

#Self Study 7-1

aa = []
for i in range(0,10) :
    aa.append(0)
    
hap = 0

for i in range(0,10) :
    aa[i] = int(input(str(i+1) + "๋ฒˆ์งธ ์ˆซ์ž : "))
    
i=0
while i < 10 :
    hap += aa[i]
    i+=1
    
#hap = aa[0] + aa[1] + aa[2] + aa[3]

print("ํ•ฉ๊ณ„ ==> %d" % hap)

[Self Study 7-2]

#7-4

aa = []
bb = []
value = 0

for i in range(0,100): 
    aa.append(value)
    value += 2
    
for i in range(0,100):
    bb.append(aa[99-i])

print("bb[0]์—๋Š” %d์ด b[99]์—๋Š” %d์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค." % (bb[0],bb[99]))
#Self study 7-2

aa = []
bb = []
value = 0

for i in range(0,200): #3์˜ ๋ฐฐ์ˆ˜ 200๊ฐœ ์ž…๋ ฅ #3์˜ ๋ฐฐ์ˆ˜ ๋ฐ›๊ธฐ
    aa.append(value)
    value += 3
    
for i in range(0,200):
    bb.append(aa[199-i])

print("bb[0]์—๋Š” %d์ด b[199]์—๋Š” %d์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค." % (bb[0],bb[199]))
aa = []
for i in range(0,10):#10๊ฐœ ์ž…๋ ฅ์œผ๋กœ ๋ณ€๊ฒฝ
    aa.append(0)
hap = 0

for i in range(0,10):#ํ•ฉ๊ณ„ ๊ตฌํ•˜๋Š” ๋งˆ์ง€๋ง‰์€ while๋ฌธ ์‚ฌ์šฉ
   aa[i] = int(input(str[i+1]) + "๋ฒˆ์งธ ์ˆซ์ž : ")

while i<10 :
  hap = aa[i]
 
 # hap = aa[0] + aa[1] + aa[2] + aa[3]

 print("ํ•ฉ๊ณ„ ==> %d" % hap)

โ–ถ ํŠœํ”Œ ๊ฐœ๋…

  • ํŠœํ”Œ์€ ์†Œ๊ด„ํ˜ธ () ๋กœ ์ƒ์„ฑํ•œ๋‹ค.
  • ํŠœํ”Œ์€ ๊ฐ’์„ ์ˆ˜์ •, ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์—†์Œ ⇒ ์ฝ๊ธฐ ์ „์šฉ
tt1 = (10,20,30); tt1  #๋’ค์— ํŠœํ”Œ๋ช…์€ ์ถœ๋ ฅํ•˜๋ ค๊ณ  ์ ์–ด๋‘” ๊ฒƒ
tt2 = 10,20,30; tt2

# ํ•ญ๋ชฉ ํ•˜๋‚˜์ธ ํŠœํ”Œ์€ ์‰ผํ‘œ(,) ๊ผญ ๋ถ™์ธ๋‹ค.
tt3 =(10); #10 => ํŠœํ”Œ ์•„๋‹˜
tt4 =10; #10  => ํŠœํ”Œ ์•„๋‹˜
tt5 =(10,); #(10,)

#์†Œ๊ด„ํ˜ธ () ์ƒ๋žต ๊ฐ€๋Šฅํ•จ
tt6 =10,; #(10,)

โ–ถ ํŠœํ”Œ ์‚ญ์ œ

del(tt1)
del(tt2)

โ–ถ ํŠœํ”Œ ์‚ฌ์šฉ

tt1 = (1,2,3)
tt2 =('A','B')

# ๋Œ€๊ด„ํ˜ธ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ => ํŠœํ”Œ๋ช…[์œ„์น˜]
tt1[0] #1
tt1[0]+tt1[[1]+tt1[2] #6

# : ์ฝœ๋ก  ์ ‘๊ทผ
tt1[1:3]
tt1[1:]

#ํŠœํ”Œ ๋ง์…ˆ
tt1 + tt2 #์—ฐ๊ฒฐ๋จ

#ํŠœํ”Œ ๊ณฑ์…ˆ
tt2 * 3 #tt2๋ฅผ 3๋ฒˆ ์—ฐ์† ์ถœ๋ ฅ

โ–ถ ํŠœํ”Œ ์ˆ˜์ • ( ํŠœํ”Œ → ๋ฆฌ์ŠคํŠธ → ํŠœํ”Œ ๋ณ€ํ™˜)

#ํŠœํ”Œ์„ ์ค‘๊ฐ„์— ๋ฐ”๊พธ๊ณ  ์‹ถ์„ ๋•Œ๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ”๊ฟ”์„œ ๋ณ€๊ฒฝํ•œ๋‹ค.

aTuple = (10,20)
aList = list(aTuple) # aํŠœํ”Œ์„ a๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€๊ฒฝ
aList.append(30)
aTuple = tuple(aList)
aTuple # 10,20,30

โ–ถ๋”•์…”๋„ˆ๋ฆฌ (๋‹ค๋ฅธ ์–ธ์–ด์—์„  ํ•ด์‹œ, ์—ฐ๊ด€ ๋ฐฐ์—ด ์ด๋ผ๊ณ  ํ•จ )

  • ์Œ 2๊ฐœ๊ฐ€ ํ•˜๋‚˜๋กœ ๋ฌถ์ธ ์ž๋ฃŒ๊ตฌ์กฐ ⇒ ํ‚ค Key + ๊ฐ’ Value
  • ์ค‘๊ด„ํ˜ธ {} ๋ฌถ์–ด์„œ ๊ตฌ์„ฑํ•œ๋‹ค.
# ์ˆœ์„œ๊ฐ€ ์—†์Œ 
dic1 = {1:'a',2:'b'}  //ํ‚ค, ๊ฐ’
dic2 = {'a':1,'b':2}  //ํ‚ค, ๊ฐ’
# ์œ ์ผ์„ฑ์„ ๋งŒ์กฑํ•ด์•ผํ•จ (์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ์œผ๋ฉด ์•ˆ๋จ)
# ๋™์ผํ•œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰์— ์žˆ๋Š” ํ‚ค๊ฐ€ ์ ์šฉ๋จ
dic1 = {1:'a',2:'b',1:'c'} 

โ–ถ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ

๋”•์…”๋„ˆ๋ฆฌ ๋ณ€์ˆ˜ = {ํ‚ค1:๊ฐ’1, ํ‚ค2:๊ฐ’2 }

โ–ถ๋”•์…”๋„ˆ๋ฆฌ ์ถ”๊ฐ€ (์ธ๋ฑ์Šค ์ด์šฉ)

student1['์—ฐ๋ฝ์ฒ˜'] = '010-1111-1111'

โ–ถ๋”•์…”๋„ˆ๋ฆฌ ์ˆ˜์ • (์ธ๋ฑ์Šค ์ด์šฉ)

student1['์—ฐ๋ฝ์ฒ˜'] = '010-1111-1111'

โ–ถ๋”•์…”๋„ˆ๋ฆฌ ์‚ญ์ œ

del(student1['์—ฐ๋ฝ์ฒ˜'])

โ–ถ๋”•์…”๋„ˆ๋ฆฌ ์‚ฌ์šฉ

##ํ‚ค๋กœ ๊ฐ’์— ์ ‘๊ทผํ•˜๋“  get ์ด์šฉํ•˜๋“  ๊ฒฐ๊ณผ๋Š” ๊ฐ™์Œ##

# ํ‚ค๋กœ ๊ฐ’์— ์ ‘๊ทผ
# ์—†๋Š” ํ‚ค ํ˜ธ์ถœ ์‹œ ์˜ค๋ฅ˜!!!
student1['์—ฐ๋ฝ์ฒ˜'] 

#๋”•์…”๋„ˆ๋ฆฌ๋ช….get(ํ‚ค)  ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ ‘๊ทผ   
#์—†๋Š” ํ‚ค ํ˜ธ์ถœ ์‹œ ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ X
student1.get('์—ฐ๋ฝ์ฒ˜') 
#๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ํ‚ค ๋ฐ˜ํ™˜
student1.keys() #dict_keys(['์—ฐ๋ฝ์ฒ˜','์ด๋ฆ„'])
								

#dict_key  ์—†์ด ์ถœ๋ ฅ 
list(student1.keys()) #['์—ฐ๋ฝ์ฒ˜','์ด๋ฆ„']

#ํŠœํ”Œํ˜•ํƒœ๋กœ ์ถœ๋ ฅ => ๋”•์…”๋„ˆ๋ฆฌ๋ช….items() 
student1.items()  #dict_items([('์—ฐ๋ฝ์ฒ˜','010-333-333'),('์ด๋ฆ„','ํ™๊ธธ๋™')])

#ํ‚ค์˜ ์œ ๋ฌด ํ™•์ธ => in 
'์ด๋ฆ„' in student1    #True
'๋ณ„๋ช…' in student1    #False
#7-8

singer = {}
	singer['์ด๋ฆ„'] = 'ํŠธ์™€์ด์Šค'

for k in singer.keys(): // k ๊ฐ’์ด singer.keys() ๋ฆฌํ„ด๊ฐ’ (4๊ฐœ)๊นŒ์ง€ 
	print('%s --> %s' % (k, singer[k]))

โ–ถ๋”•์…”๋„ˆ๋ฆฌ ์ •๋ ฌ

# 7-9 
# ๊ฐ’์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ
# ๋ฆฌ์ŠคํŠธ์•ˆ์— ํŠœํ”Œ๋กœ ๋”•์…”๋„ˆ๋ฆฌ ํ•ญ๋ชฉ ๋ณ€๊ฒฝ๋จ
import operator

trainDic, trainList = {}, []

trainDic = {'Thomas' : 'ํ† ๋งˆ์Šค', 'Edward' : '์—๋“œ์›Œ๋“œ' , 'Henry' : 'ํ—จ๋ฆฌ', 'Gothen' : '๊ณ ๋“ '}
trainList = sorted(trainDic.items(), key = operator.itemgetter(0))

print(trainList)

[ํ”„๋กœ๊ทธ๋žจ 1] 7-7

[ํ”„๋กœ๊ทธ๋žจ 2] 7-10 ๋”•์…”๋„ˆ๋ฆฌ ์ด์šฉํ•ด์„œ ์Œ์‹ ๊ถํ•ฉ ์ถœ๋ ฅ

##๋ณ€์ˆ˜ ์„ ์–ธ ๋ถ€๋ถ„##
foods = {"๋–ก๋ณถ์ด":"์˜ค๋Ž…", 
        "์งœ์žฅ๋ฉด":"๋‹จ๋ฌด์ง€",
        "๋ผ๋ฉด":"๊น€์น˜",
        "ํ”ผ์ž":"ํ”ผํด",
        "๋งฅ์ฃผ":"๋•…์ฝฉ",
        "์น˜ํ‚จ":"์น˜ํ‚จ๋ฌด",
        "์‚ฝ๊ฒน์‚ด":"์ƒ์ถ”",
        }

##๋ฉ”์ธ ์ฝ”๋“œ ๋ถ€๋ถ„##

while(True) :
  myfood = input(str(list(foods.keys()))+"์ค‘ ์ข‹์•„ํ•˜๋Š” ์Œ์‹์€? ")
  if myfood in foods :
    print("<%s> ๊ถํ•ฉ ์Œ์‹์€ <%s> ์ž…๋‹ˆ๋‹ค." %(myfood, foods.get(myfood)))
  elif myfood == "๋" :
    break;
  else :
    print("๊ทธ๋Ÿฐ ์Œ์‹์ด ์—†์Šต๋‹ˆ๋‹ค. ํ™•์ธํ•ด ๋ณด์„ธ์š”.")

SELF STUDY 7-3

SELF STUDY 7-4

SELF STUDY 7-5

โ–ถ์„ธํŠธ

  • ํ‚ค๋งŒ ๋ชจ์•„ ๋†“์€ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํŠน์ˆ˜ํ•œ ํ˜•ํƒœ
  • ํ‚ค๋Š” ์ค‘๋ณต ๋˜๋ฉด ์•ˆ๋จ == ์œ ์ผ์„ฑ
  • ๋งŒ์•ฝ ์ค‘๋ณต๋˜๋ฉด ์ž๋™์œผ๋กœ ํ•˜๋‚˜๋งŒ ๋‚จ์Œ (๋งจ ๋’ค ๊ฐ’)
  • ๋”•์…”๋„ˆ๋ฆฌ ์ฒ˜๋Ÿผ ์ค‘๊ด„ํ˜ธ {} ์‚ฌ์šฉ , ํ•˜์ง€๋งŒ : ์ฝœ๋ก ์€ ์—†์ด
mySet1 = {1,2,3,4}
  • ์ˆ˜๋Ÿ‰์ด ์•„๋‹Œ ์ข…๋ฅ˜๋งŒ ํŒŒ์•… ํ•˜๊ณ  ์‹ถ์„ ๋•Œ
set(salesList) // ์ค‘๋ณต๋œ ๊ฐ’์„ ๋บ€๋‹ค.
  • ์‹ฌํ™”๋‚ด์šฉ