๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT ๐Ÿ’ป/Python

[Python] ์ž๋ฃŒํ˜•_๋ฆฌ์ŠคํŠธ ๊ฐœ๋… ๋ฐ ํ•จ์ˆ˜ ํ™œ์šฉ๋ฒ•

by Daphne_Kim 2022. 2. 28.
SMALL
๋ฆฌ์ŠคํŠธ์˜ ๊ฐœ๋…

๋ฆฌ์ŠคํŠธ๋Š” ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•ด ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‚˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ•ญ๋ชฉ๋“ค์„ ๋ฌถ์–ด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋‹ค.

๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด๊ฐ€๋Š” ํ•ญ๋ชฉ์€ ์ˆซ์ž, ๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ์„ธํŠธ, ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ํ˜•ํƒœ๋”๋ผ๋„ ๋นˆ๋ฆฌ์ŠคํŠธ๋Š” ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ๋‹ค.

 

๋ฆฌ์ŠคํŠธ ์•ˆ์— ํ•ญ๋ชฉ(์š”์†Œ)๋Š” i(Index)๋ผ๊ณ  ํ•˜๋ฉฐ, ๋ณ€์ˆ˜๋ช…[i]๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

n๊ฐœ์˜ ํ•ญ๋ชฉ์ด ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ธ๋ฑ์Šค i์˜ ๋ฒ”์œ„๋Š” 0 ๋ถ€ํ„ฐ n-1๊นŒ์ง€์ด๋‹ค. 

๋ฆฌ์ŠคํŠธ ํ™œ์šฉ๋ฒ• 

์›ํ•˜๋Š” ํŠน์ • ํ•ญ๋ชฉ๋งŒ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์Šฌ๋ผ์ด์‹ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ๋‹ค๋งŒ ํŠน์ • ํ•ญ๋ชฉ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•  ๊ฒฝ์šฐ

๊ฐ๊ฐ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋งŒ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๋ฒˆ์— ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด ํŒ๋‹ค์Šค ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด ์กฐ๊ฑด์— ๋งž๋Š” ์ปฌ๋Ÿผ์—์„œ ๋‹ค์–‘ํ•œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. (์ด ๋ถ€๋ถ„์€ ๋”ฐ๋กœ ์—…๋กœ๋“œํ•  ์˜ˆ์ •์ด๋‹ค.)

# ์›ํ•˜๋Š” ์ผ๋ถ€ ํ•ญ๋ชฉ ๊ฐ€์ ธ์˜ค๊ธฐ
print(studynum[0]) #0๋ฒˆ์งธ ํ•ญ๋ชฉ ์ถœ๋ ฅ
print(studynum[:2]) #2๋ฒˆ์งธ ์ง์ „ ํ•ญ๋ชฉ๊นŒ์ง€ ์ถœ๋ ฅ
print(studynum[1:4]) #1๋ฒˆ์งธ ํ•ญ๋ชฉ๋ถ€ํ„ฐ 4๋ฒˆ์งธ ํ•ญ๋ชฉ ์ง์ „๊นŒ์ง€ ์ถœ๋ ฅ
print(studynum[1:]) #1๋ฒˆ์งธ ํ•ญ๋ชฉ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ถœ๋ ฅ
print(studynum[-1]) #-์˜ ๊ฒฝ์šฐ ์˜ค๋ฅธ์ชฝ์—์„œ -1๋ถ€ํ„ฐ ์‹œ์ž‘

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
1
[1, 2]
[2, 3, 4]
[2, 3, 4, 5, 6]
6

# ํŠน์ • ํ•ญ๋ชฉ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ
studynum[0] = 100
print(studynum)

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
[100, 2, 3, 4, 5, 6]

del์„ ์ด์šฉํ•ด ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ • ํ•ญ๋ชฉ, ํ˜น์€ ๋ฆฌ์ŠคํŠธ ์ž์ฒด๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. 

# ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ญ๋ชฉ ์‚ญ์ œํ•˜๊ธฐ
list_con1 = [1, 2, 3]
list_con2 = ['๋ฒ”์ด', '๋‚ด๋ ค', '์˜จ๋‹ค']
list_con3 = list_con1 + list_con2

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
[1, 2, 3, '๋ฒ”์ด', '๋‚ด๋ ค', '์˜จ๋‹ค'] # ํ•ญ๋ชฉ ์‚ญ์ œํ•˜๊ธฐ ์ „ ๊ฒฐ๊ณผ

del list_con3[2:]
print(list_con3)

# ์‚ญ์ œ ํ›„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ
[1, 2]

ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ ์•ˆ์— ํŠน์ • ํ•ญ๋ชฉ์ด ์กด์žฌํ•˜๋Š” ์ง€ ํ™•์ธํ•˜๋ ค๋ฉด ํ•ญ๋ชฉ in ๋ฆฌ์ŠคํŠธ๋ช…์„ ์‚ฌ์šฉํ•ด์ค€๋‹ค.

# ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ญ๋ชฉ์˜ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ
# ํ•ญ๋ชฉ in ๋ฆฌ์ŠคํŠธ
# ํ•ญ๋ชฉ์ด ์žˆ์œผ๋ฉด True ์—†์œผ๋ฉด False๋ฐ˜ํ™˜

list_data1 = [1, 2, 31, 24, 5]
print(2 in list_data1) #True
print(39 in list_data1) #False
๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ_์‚ฝ์ž…
# ์˜ˆ์ œ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ: ์ง€ํ•˜์ฒ ์— (์ตœ์• )๋งˆ๋ธ” ๋ฉค๋ฒ„๋“ค์ด ํƒ”๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž
# ์ŠคํŒŒ์ด๋”๋งจ, ์•„์ด์–ธ๋งจ, ๋ธ”๋ž™ํŒฌ์„œ, ๋‹ฅํ„ฐ์ŠคํŠธ๋ ˆ์ธ์ง€

subway = ['Spyderman', 'Ironman', 'BlackPanther', 'Dr.Strange']
print(subway)

#1. index ํ•จ์ˆ˜: ์ธ์ž์™€ ์ผ์น˜ํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ์ฐพ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
print(subway.index('Ironman')) # ์ถœ๋ ฅ ๊ฒฐ๊ณผ 1๋ฒˆ์งธ

#2. append ํ•จ์ˆ˜: ๋ฆฌ์ŠคํŠธ ๋งจ ๋’ค์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
subway.append('Thor')
print(subway)

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
['Spyderman', 'Ironman', 'BlackPanther', 'Dr.Strange', 'Thor']

#3. insert ํ•จ์ˆ˜: ํŠน์ • ์œ„์น˜์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
# !๋ช‡ ๋ฒˆ์งธ ์š”์†Œ์— ์œ„์น˜ํ•˜๋Š” ์ง€ ๋จผ์ € ์ ์–ด์ค˜์•ผ ํ•จ!
subway.insert(0, 'Wanda')
print(subway)

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
['Wanda', 'Spyderman', 'Ironman', 'BlackPanther', 'Dr.Strange', 'Thor']

#4. extend ํ•จ์ˆ˜
# - ๋ฆฌ์ŠคํŠธ ๋งจ ๋์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
# - ๋‘ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜๋กœ ๊ฒฐํ•ฉํ•ด ํ™•์žฅํ•˜๊ธฐ๋„ ํ•œ๋‹ค

# ๋ฆฌ์ŠคํŠธ ํ™•์žฅ์„ ์œ„ํ•ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋˜ ์ƒ์„ฑํ•ด์ฃผ์ž!
# ์ง€ํ•˜์ฒ ์— ๋‹ค๋ฅธ ์„ธ๊ณ„๊ด€์˜ ํžˆ์–ด๋กœ๋“ค๊ณผ ๋นŒ๋Ÿฐ๋“ค์ด ํƒ‘์Šนํ–ˆ๋‹ค
Hero = ['Betman', 'Wonder Women']
Villain = ['Joker', 'Bane']
subway.extend(Hero + Villain)
print(subway)

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
['Wanda', 'Spyderman', 'Ironman', 'BlackPanther', 'Dr.Strange', 'Thor', 
'Betman', 'Wonder Women', 'Joker', 'Bane']
๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ_์‚ญ์ œ
#1. pop ํ•จ์ˆ˜:
# - ๋ฆฌ์ŠคํŠธ์—์„œ ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•œ ํ›„์— ๋ฐ˜ํ™˜
# - ๋งจ ๋’ค์—์„œ๋ถ€ํ„ฐ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ์ œ๊ฑฐํ•  ๋•Œ ์‚ฌ์šฉ

(subway.pop())
print(subway) 

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
['Wanda', 'Spyderman', 'Ironman', 'BlackPanther', 'Dr.Strange']

#2. clear ํ•จ์ˆ˜: ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ ๊ฐ’ ์ „๋ถ€ ์ œ๊ฑฐํ•˜๊ธฐ(๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋จ)
Villain.clear()
print(Villain) # ์ถœ๋ ฅ ๊ฒฐ๊ณผ: ()

#3. remove ํ•จ์ˆ˜: ์ž…๋ ฅ๊ฐ’๊ณผ ์ฒซ๋ฒˆ์งธ๋กœ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์„ ์‚ญ์ œ
Hero.remove('Superman')
print(Hero)

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
['Betman', 'Wonder Women']
๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ_๊ฐœ์ˆ˜ ์„ธ๊ธฐ, ์ •๋ ฌ
#1. count ํ•จ์ˆ˜: ํŠน์ • ๊ฐ’์ด ๋ช‡ ๋ฒˆ ๋ฐ˜๋ณต๋๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

# 3์ด ๋ช‡๋ฒˆ ๋ฐ˜๋ณต๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
num_list = [12, 3, 3, 6, 7]
print(num_list.count(3)) # ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 2

#2. sort ํ•จ์ˆ˜: ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ธ๋ฑ์Šค๋ฅผ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค. 
num_list.sort()
print(num_list)

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
[3, 3, 6, 7, 12]

#3. reverse ํ•จ์ˆ˜: ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ธ๋ฑ์Šค๋ฅผ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
num_list.reverse()
print(num_list)

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
[12, 7, 6, 3, 3]
SMALL