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

2020. 12. 28. 19:56ใ†๐Ÿ“š Algorithm/๐Ÿ“• Python ๋ฌธ๋ฒ•

๋ฐ˜์‘ํ˜•

ํŒŒ์ด์ฌ์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฃŒํ˜•์ธ ๋ฆฌ์ŠคํŠธ(List)์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

๋ฆฌ์ŠคํŠธ์˜ ๋ช‡๋ช‡ ๊ธฐ๋Šฅ๋“ค์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•ด๋ณผ๊ฒŒ์š”!

 

๐Ÿ‘€ List ์ธ๋ฑ์‹ฑ/์Šฌ๋ผ์ด์‹ฑ ๊ธฐ๋Šฅ

๋ฆฌ์ŠคํŠธ์—์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ํ•˜๋‚˜ ๋˜๋Š” ์—ฌ๋Ÿฌ๊ฐœ ์ฐพ๊ณ  ์‹ถ๋‹ค๋ฉด ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ์„ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

โœ”๏ธindex ๊ฐ’์— -1์„ ๋„ฃ์œผ๋ฉด list์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์‹ฑ

a = [1,2,3]
a[0] # 1
a[0] + a[1] # 3
a[-1] # 3

 

๋ฆฌ์ŠคํŠธ ์Šฌ๋ผ์ด์‹ฑ

a = [1,2,3]
a[1:] # [2,3]
a[:1] # [1,2]

 

๐Ÿ‘€List์—์„œ ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ์œ„์น˜ ์ฐพ๊ธฐ ( index )

๋ฆฌ์ŠคํŠธ์—์„œ๋Š” list.index(value)๋ฅผ ์ด์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ๊ฐ’์ด ๋ฆฌ์ŠคํŠธ ๋ช‡๋ฒˆ์งธ์— ์กด์žฌํ•˜๋Š” ์ง€ ๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

a = [1,2,3,4,5]
a.index(2) # 1
a.index(7) # 7์€ a์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ValueError ๋ฐœ์ƒ

 

๐Ÿ‘€List์—์„œ ์›ํ•˜๋Š” ์œ„์น˜์— ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ ( insert )

๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ append๊ฐ€ ์žˆ์ง€๋งŒ, append๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ๋งˆ์ง€๋ง‰์— ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ž…๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์›ํ•˜๋Š” ์œ„์น˜์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”? ๋ฐ”๋กœ list.insert( index, value ) ๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ์œ„์น˜์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

a = [1,2,3,4,5]
a.insert(0,7) # [7,1,2,3,4,5]
a.insert(2,3) # [7,1,3,2,3,4,5]

insert( index, value )๋Š” index ๋ฒˆ์งธ ์œ„์น˜์— value๋ฅผ ์‚ฝ์ž… ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ‘€ List ๋’ค์— ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ / ํ™•์žฅ ( extend )

extend๋Š” ๋ฆฌ์ŠคํŠธ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์œผ๋กœ ๋ฆฌ์ŠคํŠธ ๋’ค์— ๋ฆฌ์ŠคํŠธ ๊ฐ’ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ž…๋‹ˆ๋‹ค.

+ ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋”ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ extend๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๋Š” ๊ฒƒ๋งŒ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค : )

โœ”๏ธ list.extend( x )์—์„œ x๋Š” ๋ฆฌ์ŠคํŠธ๋งŒ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ ์„ ์ฃผ์˜ํ•˜์„ธ์š”!

a = [1,2,3]
b = [4,5]

a + b # [1,2,3,4,5]
a += [4,5] # [1,2,3,4,5]
a.extend(b) # [1,2,3,4,5]

 

๐Ÿ‘€List์—์„œ ๊ฐ’ ์ œ๊ฑฐํ•˜๊ธฐ ( remove )

๋ฆฌ์ŠคํŠธ์—์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋ ค๋ฉด remove๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹จ,  list.remove( value )๋Š” ๋ฆฌ์ŠคํŠธ์— ๊ทธ ๊ฐ’์ด ์—ฌ๋Ÿฌ๋ฒˆ ์กด์žฌํ•œ๋‹ค๋ฉด, ์ฒซ ๋ฒˆ์งธ (์ฆ‰, list์—์„œ ์•ž ์ˆœ์„œ์— ์กด์žฌํ•˜๋Š”) ๊ฐ’๋งŒ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ์ ์„ ๋ช…์‹ฌํ•ด์ฃผ์„ธ์š”!

a = [1,2,3,4,3,5]
a.remove(1) # [2,3,4,3,5]
a.remove(3) # [2,4,3,5]

 

๐Ÿ‘€List์—์„œ ๊ฐ’ ๊บผ๋‚ด๊ธฐ ( pop )

์–ธ๋œป ๋ณด๋ฉด remove์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ๋ณด์‹ค ์ˆ˜ ์žˆ๋Š” pop์€ ์›ํ•˜๋Š” ๊ฐ’์„ ๊บผ๋‚ผ ๋•Œ ์‚ฌ์šฉ๋˜์–ด์ง‘๋‹ˆ๋‹ค.

remove๋Š” ์›ํ•˜๋Š” ๊ฐ’์„ ์ œ๊ฑฐ๋งŒ , pop์€ ์›ํ•˜๋Š” ๊ฐ’์„ ๊บผ๋‚ด์–ด ์ œ๊ฑฐํ•œ๋‹ค๊ณ  ์ •๋ฆฌํ•˜๋ฉด ๋ ๊ฑฐ ๊ฐ™์•„์š”.

list.pop(index) or list.pop()์€ index ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๋ฆฌ์ŠคํŠธ์˜ index ๋ฒˆ์งธ์˜ ๊ฐ’์„ ๋Œ๋ ค์ฃผ๊ณ  ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

โœ”๏ธpop์€ index์™€ ๋‹ค๋ฅด๊ฒŒ ์›ํ•˜๋Š” ๊ฐ’์ด ์•„๋‹Œ ์œ„์น˜๋กœ ๊ฐ’์„ ๊บผ๋‚ด์ค€๋‹ค๋Š” ์  ์ฃผ์˜ํ•ด์ฃผ์„ธ์š” ~

a = [1,2,3,4,3,5]
a.pop() # ๋งจ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๋Œ๋ ค์คŒ - 5 , a = [1,2,3,4,3]
a.pop(1) # ๋ฆฌ์ŠคํŠธ์˜ 1 ๋ฒˆ์งธ ๊ฐ’์„ ๋Œ๋ ค์คŒ - 2 , a = [1,3,4,3]

 

๐Ÿ‘€List์— ํฌํ•จ๋œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ ( count )

๋ฆฌ์ŠคํŠธ์—์„œ ๋งŒ์•ฝ '์›ํ•˜๋Š” ๊ฐ’'์ด ๋ช‡๊ฐœ ๋“ค์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ๋•Œ์—๋Š” count๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์›ํ•˜๋Š” ๊ฐ’์ด ๋ฆฌ์ŠคํŠธ์•ˆ์— ์—†๋‹ค๋ฉด 0์„ ๋ฆฌํ„ดํ•ด ์ค๋‹ˆ๋‹ค.

a = [1,2,3,4,3,5]
a.count(1) # 1
a.count(3) # 2
a.count(7) # 0 

 

โœ”๏ธList์—์„œ ์ „์ฒด ๊ฐฏ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ?! len( ) ์„ ์ด์šฉํ•˜์ž!

a = [1,2,3,4,3,5]
len(a) # 6

 

๐Ÿ‘€List์—์„œ ์ •๋ ฌํ•˜๊ธฐ ( sort / reverse )

๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์„ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด์—์„œ ๋งค์šฐ ๋งŽ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค!

๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด sort ๋ฅผ, ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ์‹œ์—๋Š” reverse๋ฅผ ์ด์šฉํ•˜์„ธ์š”.

 

 

๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ ( sort ) - ๊ธฐ๋ณธ ๊ฐ’ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

a = [1,2,3,4,3,5]
a.sort() # [1,2,3,3,4,5]

b = ['a','c','b']
b.sort() # ['a','b','c']

 

๊ฐ’์„ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ ( reverse )

a = [1,2,3,4,3,5]
a.reverse() [5,3,4,3,2,1]

b = ['a','c','b']
b.reverse() # ['b','c','a']

โœ”๏ธ reverse๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์ด ์•„๋‹Œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘๋Š” ๊ฒƒ ๋ฟ์ž„์„ ์ฃผ์˜ํ•˜์„ธ์š”!!

 

 

 

๐Ÿ“Œ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋งŽ์ด ํ™œ์šฉํ•  List ๊ด€๋ จ ํ•จ์ˆ˜๋“ค์—๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด์•˜๋Š”๋ฐ์š”.

๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•˜์‹ค ๋•Œ ์œ„ ํ•จ์ˆ˜๋“ค์„ ์ด์šฉํ•˜๋ฉด ์ข€ ๋” ํŽธํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค : )

List์˜ ์ •๋ ฌ์€ ์ž์ฃผ ์“ฐ์ด๋Š” ๋ถ€๋ถ„์ด๊ณ  ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ˆ ๋”ฐ๋กœ ์ •๋ฆฌํ•ด๋‘๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

 

 

 

๋ฐ˜์‘ํ˜•