๋ฆฌ์ŠคํŠธ(List) / ๋”•์…”๋„ˆ๋ฆฌ(Dictionary) ์ •๋ ฌ

2020. 12. 29. 00:01ใ†๐Ÿ“š Algorithm/๐Ÿ“• Python ๋ฌธ๋ฒ•

๋ฐ˜์‘ํ˜•

 

์ง€๋‚œ ๊ธ€์— ์ด์–ด ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ์— ๊ด€ํ•ด ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค!

 

๋ฆฌ์ŠคํŠธ(list) ์ž๋ฃŒํ˜•๊ณผ ๋”•์…”๋„ˆ๋ฆฌ(dictionary) ์ž๋ฃŒํ˜•์— ๋Œ€ํ•ด ํ—ท๊ฐˆ๋ฆฌ์‹ ๋‹ค๋ฉด ?!

์•„๋ž˜ ๊ธ€์„ ์ฝ๊ณ  ์˜ค์‹œ๋ฉด ์ดํ•ดํ•˜์‹œ๊ธฐ ๋” ์‰ฌ์šฐ์‹ค ๊ฑฐ์˜ˆ์š” : )

 

๋ฆฌ์ŠคํŠธ(List)

ํŒŒ์ด์ฌ์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฃŒํ˜•์ธ ๋ฆฌ์ŠคํŠธ(List)์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋ช‡๋ช‡ ๊ธฐ๋Šฅ๋“ค์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•ด๋ณผ๊ฒŒ์š”! ๐Ÿ‘€ List ์ธ๋ฑ์‹ฑ/์Šฌ๋ผ์ด์‹ฑ ๊ธฐ๋Šฅ ๋ฆฌ์ŠคํŠธ์—์„œ ์›ํ•˜๋Š” ๊ฐ’

yunaaaas.tistory.com

 

๋”•์…”๋„ˆ๋ฆฌ(Dictionary)

dic_name = {'์ด๋ฆ„':'์œ ๋‚˜','phone':'01012345678','birth':'20201228'} dic_wintable = {'๊ฐ€์œ„':'๋ณด','๋ฐ”์œ„':'๊ฐ€์œ„','๋ณด':'๋ฐ”์œ„'} ์ด์ฒ˜๋Ÿผ python์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ(Dictionary) ๋Š” ๊ฐ€์œ„๋ฐ”์œ„๋ณด ํ…Œ์ด๋ธ”..

yunaaaas.tistory.com

 

 

์ •๋ ฌ์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด์‹œ ์ž์ฃผ ํ™œ์šฉ๋˜๋ฉฐ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค!

list.sort() ์™€ sorted() ์ฐจ์ด์™€ Dictionary ์ •๋ ฌ์‹œ์— ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” lambda ์‹์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณผ๊ฒŒ์š” ~

 

 

๋ฆฌ์ŠคํŠธ(List) ์ •๋ ฌ

๋ฆฌ์ŠคํŠธ ์ •๋ ฌ์—๋Š” list ๋ณธ์ฒด๋ฅผ ์ •๋ ฌํ•˜๋Š” sort ๋ฐฉ์‹๊ณผ list ์ •๋ ฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” sorted ๋ฐฉ์‹์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์ •๋ฆฌํ•˜์ž๋ฉด, ๋ฆฌ์ŠคํŠธ a ๋งŒ ์ •๋ฆฌํ•˜๋ฉด ๋ผ! ๐Ÿ‘‰๐Ÿป sort , ๋ฆฌ์ŠคํŠธ a์˜ ์ •๋ ฌ๋  ๊ฐ’์ด ํ•„์š”ํ•ด! ๐Ÿ‘‰๐Ÿป sorted ๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ์‰ฌ์šธ ๊ฑฐ ๊ฐ™์•„์š”!

 

 

๐Ÿ‘€ ๋ฆฌ์ŠคํŠธ (list) ๋ณธ์ฒด ์ •๋ ฌ ( sort / reverse )

  • sort :  ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ  (1,2,3,4 ... ) , ๊ธฐ๋ณธ๊ฐ’์€ ์˜ค๋ฆ„ ์ฐจ์ˆœ ์ •๋ ฌ
    • reverse ์˜ต์…˜ : True ์‹œ, ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
    • key ์˜ต์…˜ : ์ง€์ •๋œ ํ•จ์ˆ˜ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ์ •๋ ฌ
  • reverse : ๋ฆฌ์ŠคํŠธ ์›์†Œ๋“ค์„ ๋ฐ˜๋Œ€๋กœ ์ •๋ ฌ (๊ฑฐ๊พธ๋กœ)
a = [1, 10, 5, 7, 6]
a.reverse(). # [6, 7, 5, 10, 1]

a = [1, 10, 5, 7, 6]
a.sort()  # [1, 5, 6, 7, 10]

a = [1, 10, 5, 7, 6]
a.sort(reverse=True)  # [10, 7, 6, 5, 1]

m = ['๋‚˜๋Š”', 'ํŒŒ์ด์ฌ์„', '์ž˜ํ•˜๊ณ ', '์‹ถ๋‹ค']
m.sort(key=len)  # ['๋‚˜๋Š”', '์‹ถ๋‹ค', '์ž˜ํ•˜๊ณ ', 'ํŒŒ์ด์ฌ์„']

 

๐Ÿ‘€ ๋ฆฌ์ŠคํŠธ (list) ์ •๋ ฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ (sorted / reversed)

๐Ÿ“Œ ์ •๋ ฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ๋งŒ ํ•  ๋ฟ, ๋ฆฌ์ŠคํŠธ์˜ ๋ณธ์ฒด๋Š” ๋ณ€ํ˜•(์ •๋ ฌ)๋˜์ง€ ์•Š๋Š” ๋‹ค๋Š” ์  ์œ ์˜ ํ•˜์„ธ์š”!

 

  • sorted : ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ , ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ 
  • reversed : ๋ฆฌ์ŠคํŠธ ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘๊ธฐ ๋‹จ, iterableํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜

 

iterableํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š”๊ฒŒ ์ดํ•ด๊ฐ€ ์–ด๋ ค์šฐ์‹ค ์ˆ˜ ์žˆ๋Š”๋ฐ reversed(x)์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ํ™•์ธํ•ด๋ณด์‹œ๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šธ ๊ฑฐ ๊ฐ™์•„์š”~

x = [1 ,11, 2, 3]
y = sorted(x) # x - [1,11,2,3]
y  # [1, 2, 3, 11]

x = [1 ,11, 2, 3]
y = reversed(x) # x - [1,11,2,3]
y # <list_reverseiterator object at 0x1060c9fd0>
list(y) # [3, 2, 11, 1]

 

reversed( )์˜ ๊ฒฐ๊ณผ๊ฐ’์€ nontype์ด ์•„๋‹ˆ๊ณ , iterator๊ฐ€ ๋˜๊ธฐ ์œ„ํ•œ ํ˜•ํƒœ์ด๋ฏ€๋กœ ์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€์ง‘๋‹ˆ๋‹ค.

๋งŒ์•ฝ reverse๋œ list๊ฒฐ๊ณผ๊ฐ’์„ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด !? ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜์‹œ์ผœ์ฃผ๋Š” list( )์„ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

๋”•์…”๋„ˆ๋ฆฌ(Dictionary) ์ •๋ ฌ

๋”•์…”๋„ˆ๋ฆฌ ์ •๋ ฌ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ •๋ ฌ ๋ฌธ์ œ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ด์ง€๋งŒ ์ž˜ ๋ชจ๋ฅด์‹œ๋Š” ๋ถ„๋“ค์ด ๋งŽ์„ ๊ฑฐ ๊ฐ™์€๋ฐ์š”!

value ๊ธฐ์ค€์˜ ์ •๋ ฌ๊ณผ key ๊ธฐ์ค€์˜ ์ •๋ ฌ์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•ด๋“œ๋ฆฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

key๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™์ด sort()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, value๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋ ค๋ฉด lambda๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ‘€ key ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ

  • sorted( dic.items () ) : ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ , ์ •๋ ฌ๋œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฆฌ์ŠคํŠธ ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜
  • sorted ( dic.items () , reverse = True) : ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ, ์ •๋ ฌ๋œ ๋”•์…”๋„ˆ๋ฆฌ ๋ฆฌ์ŠคํŠธ ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜
dic = { 
 	1 : 'b',
    	3 : 'c',
    	5 : 'd',
    	2 : 'a',
    	4 : 'e'
}

dic2 = sorted(dic.keys()) # [ 1,2,3,4,5 ]
dic3 = sorted(dic.items()) # [ (1,'b'), (2,'a'), (3,'c'), (4,'e'), (5,'d') ]

 

sorted ( ) ์•ˆ์— dic.keys ( ) ๋กœ ์ •๋ ฌํ•˜๊ฒŒ ๋˜๋ฉด ์ •๋ ฌ๋œ Key ๊ฐ’๋งŒ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜๋˜๊ณ , 

dic.items ( )๋กœ ์ •๋ ฌ์‹œ์—๋Š” ํŠœํ”Œ(Tuple) ํ˜•ํƒœ๋กœ Key๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฐ˜ํ™˜๋˜์–ด์ง‘๋‹ˆ๋‹ค!

 

๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์‹œ์—๋Š” reverse ์˜ต์…˜์„ True๋กœ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋˜์š”!

dic = { 
 	1 : 'b',
    	3 : 'c',
    	5 : 'd',
    	2 : 'a',
    	4 : 'e'
}

dic2 = sorted(dic.keys() , reverse = True) # [ 5,4,3,2,1 ]
dic3 = sorted(dic.items() , reverse = True) # [ (5,'d'), (4,'e'), (3,'c'), (2,'a'), (1,'b') ]

 

๐Ÿ‘€ value ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ

value๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋ ค๋ฉด lambda ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

sorted ํ•จ์ˆ˜์— key ์˜ต์…˜์„ lambda ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ value๊ฐ’์œผ๋กœ ์ง€์ •ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ธ๋ฐ์š” ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ์ดํ•ดํ•˜์‹œ๊ธฐ ์‰ฌ์šธ๊ฑฐ ๊ฐ™์•„์š”~

dic = { 
 	1 : 'b',
    	3 : 'c',
    	5 : 'd',
    	2 : 'a',
    	4 : 'e'
}

# [ (2,'a'),(1,'b'),(3,'c'),(5,'d'),(4,'e') ]
dic2 = sorted(dic.items(), key = lambda item : item[1]) 

 

item [1]๋กœ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์€ item[0] ์€ key๊ฐ’์ด๊ณ , value ๊ฐ’์€ item [1]์ด๊ธฐ ๋•Œ๋ฌธ์— item์„ item[1]๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค๋ฅผ lambda ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

 

๊ทธ๋ ‡๋‹ค๋ฉด lambda๋ฅผ ์ด์šฉํ•˜์—ฌ key๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์„ ๋• ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ๋ ๊นŒ์š”?!

item [0]์œผ๋กœ lamda์‹์˜ item์„ ์ง€์ •ํ•˜์—ฌ ์ •๋ ฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค!

dic = { 
 	1 : 'b',
    	3 : 'c',
    	5 : 'd',
    	2 : 'a',
    	4 : 'e'
}

# [ (1,'b'), (2,'a'), (3,'c'), (4,'e'), (5,'d') ]
dic2 = sorted(dic.items(), key = lambda item : item[0])

 

โœ”๏ธ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์‹œ key ๊ฐ’ ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ๊ณผ ๊ฐ™์ด reverse = True ์˜ต์…˜์„ ์ด์šฉํ•˜๊ฑฐ๋‚˜ ์•„๋‹ˆ๋ฉด item [0]์— - ๋ฅผ ๋ถ™์ด๋ฉด ๋ฉ๋‹ˆ๋‹ค!

 

 

๐Ÿ‘€ ๋žŒ๋‹ค์‹(lambda)์ด์šฉํ•˜์—ฌ ์ •๋ ฌ ์‘์šฉํ•ด๋ณด๊ธฐ

์•„์ง ๋žŒ๋‹ค์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ต์ˆ™ํ•˜์ง€ ์•Š์œผ์‹ค ๊ฑฐ ๊ฐ™์€๋ฐ์š” !

๊ทธ๋ž˜์„œ ๋žŒ๋‹ค์‹์„ ํ™œ์šฉํ•œ ๋”•์…”๋„ˆ๋ฆฌ ์ •๋ ฌ ์‘์šฉ ์˜ˆ์‹œ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค ~

 

student = [
	{'name':'kim', 'age' : 20},
    	{'name':'park', 'age' : 10},
        {'name':'lee', 'age' : 13},
        {'name':'choi', 'age' : 25},
        {'name':'park', 'age' : 25},
        {'name':'lee', 'age' : 33}
 ]

 

โœ”๏ธage๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ํ›„, name์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ

stu_sorted = sorted(student, key = lambda item : (-item['age'], item['name']))

'''
[ {'name':'lee', 'age' : 33},
    {'name':'choi', 'age' : 25},
    {'name':'park', 'age' : 25},
    {'name':'kim', 'age' : 20},
    {'name':'lee', 'age' : 13},
    {'name':'park', 'age' : 10} ]
'''

 

 

๐Ÿ‘€ sorted์™€ itemgetter ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌํ•˜๊ธฐ

itemgetter์„ ์‚ฌ์šฉํ•˜๋ฉด ์ข€ ๋” ์‰ฝ๊ฒŒ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

โœ”๏ธ age๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ํ›„, name์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ

from operator import itemgetter

stu_sorted = sorted(student, key = itemgetter('age','name'))

'''
[ {'name':'park', 'age' : 10},
{'name':'lee', 'age' : 13},
{'name':'kim', 'age' : 20},
{'name':'choi', 'age' : 25},
{'name':'park', 'age' : 25},
{'name':'lee', 'age' : 33} ]
'''

 

 

 

๋ฆฌ์ŠคํŠธ์™€ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ๋‹ค๋ค„๋ดค๋Š”๋ฐ์š”.

itemgetter, lambda๋ฅผ ์ด์šฉํ•˜๋ฉด ์ข€ ๋” ๋‹ค์–‘ํ•˜๊ฒŒ ์ •๋ ฌ ๋ฌธ์ œ์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค : - )

๋ฐ˜์‘ํ˜•

'๐Ÿ“š Algorithm > ๐Ÿ“• Python ๋ฌธ๋ฒ•' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ํŒŒ์ด์ฌ์„ ํŒŒ์ด์ฌ ๋‹ต๊ฒŒ  (0) 2020.12.30
๋ฆฌ์ŠคํŠธ(List)  (1) 2020.12.28
ํŠœํ”Œ(Tuple)  (0) 2020.12.28