๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿคท๐Ÿผ‍โ™€๏ธ Etc.../- C, C++, Python, Android

Python ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ๊ฒƒ

by Wonit 2019. 12. 2.

๋ฆฌ์ŠคํŠธ

๋ฆฌ์ŠคํŠธ๋ž€ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ํŒŒ์•…ํ•˜๋Š”๋ฐ ์œ ๋ฆฌํ•œ ์ผ์ข…์˜ ์ž๋ฃŒ๊ตฌ์กฐ ์ด๋‹ค.

์šฐ๋ฆฌ๋Š” ์ „์— ๋ฌธ์ž์—ด ์‹œํ€€์Šค๋ฅผ ๋ฐฐ์› ๋Š”๋ฐ ๋ฌธ์ž์—ด ์‹œํ€€์Šค๋Š” ๋ถˆ๋ณ€ํ˜•์ด์ง€๋งŒ ๋ฆฌ์ŠคํŠธ๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋”ฐ.
๋˜ํ•œ ๋ฆฌ์ŠคํŠธ๋Š” ์ค‘๋ณต๋œ ๊ฐ’์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฆฌ์ŠคํŠธ ์ƒ์„ฑํ•˜๊ธฐ : [] , list()

๋ฆฌ์ŠคํŠธ๋Š” 0 ํ˜น์€ ๊ทธ ์ด์ƒ์˜ ์š”์†Œ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค. (,)๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ๋Œ€๊ด„ํ˜ธ`[]`๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ๋‹ค.

๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ : [] ์š”์†Œ ์ง€์ •

>>> empty_list = []
>>> name_list = ['jang', 'kim', 'jang']
>>> name_list
['jang','kim','jang']

๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ : list() ํ•จ์ˆ˜ ์ด์šฉ

>>> empty_list = list()
>>> empty_list
[]
๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ list comprehension์ด ์žˆ์ง€๋งŒ ์ถ”ํ›„์— ์„ค๋ช…ํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค!

์œ„์˜ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์šฐ๋ฆฌ๋Š” ๋‘ ๊ฐ€์ง€ ํŠน์„ฑ์„ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ

1. ๋ฆฌ์ŠคํŠธ๋Š” ์ค‘๋ณต๋œ ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
2. ๋ฆฌ์ŠคํŠธ์—๋Š” ๊ฐ’์ด ์—†์„ ์ˆ˜ ์žˆ๋‹ค.

๋ฅผ ๊ธฐ์–ตํ•˜์ž.

๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ : list()

๋ฐ”๋กœ ๋‹ค์Œ์— ๋ฐฐ์šธ ๋‚ด์šฉ์ธ ํŠœํ”Œ ๋˜ํ•œ ์ž๋ฃŒํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ ์ˆซ์ž, ๋ฌธ์ž์—ด ์‹œํ€€์Šค ๋“ฑ์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

>>> list('cat')
['c','a','t']

split()์˜ ๋™์ž‘๊ณผ ๋ฆฌ์ŠคํŠธ

๋ฐ”๋กœ ์ „์—์„œ ๋ดค๋˜ ๋ฌธ์ž์—ด ์กฐ์ž‘ ์—์„œ ์šฐ๋ฆฌ๋Š” split() ํ•จ์ˆ˜๊ฐ€ ๊ธฐ์–ต๋‚˜๋Š”๊ฐ€? ๊ทธ split()ํ•จ์ˆ˜ ๋˜ํ•œ ๋ฌธ์ž์—ด์„ ๊ตฌ๋ถ„์ž๋กœ ๋‚˜๋ˆ„์–ด ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

>>> my_birth = `2019/12/03'
>>> my_birth.split('/')
['2019', '12', '03']

[offset]์œผ๋กœ ํ•ญ๋ชฉ ์–ป๊ธฐ

๋ฌธ์ž์—ด๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฆฌ์ŠคํŠธ๋Š” ์‹œํ€€์Šค์˜ ์ผ์ข…์ด๊ธฐ ๋•Œ๋ฌธ์— offset์ด ์กด์žฌํ•œ๋‹ค. ๊ทธ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ’์„ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> friend_group = ['jang', 'kim', 'jung']
>>> friend[0]
'jang'
>>> friend[1]
'kim'

๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ

๋ฆฌ์ŠคํŠธ์— ์›์†Œ์—๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋Š”๋ฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๋ชจ๋‘ ๊ฐ์ฒด์ด๋ฏ€๋กœ int, bool, list ๋“ฑ๋“ฑ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ฐ์ฒด๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ list์— list ๋˜ํ•œ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

>>> fruit = ['apple', 'banana', 'mango']
>>> life = ['flower', 'human', fruit]
>>> life
['flower', 'human', ['apple', 'banana', 'mango']]

์œ„์˜ ๋ฌธ์žฅ์ด ์ดํ•ด๊ฐ€ ๊ฐ€๋Š”๊ฐ€?

fruit = ['apple', 'banana', 'mango']์—์„œ ๊ณผ์ผ์˜ ์ข…๋ฅ˜๋ฅผ ๋ฆฌ์ŠคํŠธ ํ™” ์‹œํ‚ค๊ณ , life = ['flower', 'human', fruit\]์—์„œ flower์ด๋ผ๋Š” ์›์†Œ์™€ human์ด๋ผ๋Š” ์›์†Œ์— fruit๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ถ”๊ฐ€๋œ ๊ตฌ์กฐ์ด๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ [offset]์„ ์ด์šฉํ•˜๊ธฐ ์กฐ๊ธˆ ๊นŒ๋‹ค๋กœ์›Œ ์กŒ๋Š”๋ฐ ์ •๋ง ์žฌ๋ฐŒ๋‹ค. ํ•œ ๋ฒˆ ๋ด๋ณด์ž.

์˜ˆ๋ฅผ๋“ค์–ด

>>> flower = ['rose', 'dongbaek', 'sun flower']
>>> human = ['jang', 'hong', 'kim']
>>> fruit = ['apple', 'banana', 'mango']
>>> life = [flower, human, fruit]

flower, human, fruit๋ผ๋Š” ๋ฆฌ์ŠคํŠธ ์„ธ ๊ฐœ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ๊ฐ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ life ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋กœ ์ถ”๊ฐ€์‹œ์ผฐ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์šฐ๋ฆฌ๋Š” human์˜hong ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์„๊นŒ? ๋ฐ”๋กœ ์ด์ค‘ ์ฐธ์กฐ์˜ ๊ฐœ๋…์„ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

>>> life[0]
['rose', 'dongbaek', 'sun flower']
>>> life[1]
['jang', 'hong', 'kim']
>>> life[0][0]
['rose']

์ด์ฒ˜๋Ÿผ ์ด์ค‘ ์ฐธ์กฐ๋กœ 1์ฐจ์  ๋ฆฌ์ŠคํŠธ ์ฐธ์กฐ ํ›„ ์›์†Œ ๋ฆฌ์ŠคํŠธ์— ์ฐธ์กฐ๋ฅผ ํ•˜๋ฉด ๋œ๋‹ค.

์˜คํ”„์…‹ ๋ฐ ์Šฌ๋ผ์ด์Šค๋กœ ํ•ญ๋ชฉ ์ถ”์ถœ

๋ฆฌ์ŠคํŠธ ๋˜ํ•œ ์‹œํ€€์Šค๋ผ๋Š” ๋ง์„ ์œ„์—์„œ ํ–ˆ๋Š”๋ฐ ๊ทธ๋ ‡๋‹ค๋ฉด ์‹œํ€€์Šค์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํŠน์ • ํ•จ์ˆ˜๋“ค์„ ๋˜‘๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> fruit = ['apple', 'banana', 'mango']
>>> fruit[0:2]
['apple', 'banana', 'mango']

๋ฆฌ์ŠคํŠธ์˜ ๋์— ํ•ญ๋ชฉ ์ถ”๊ฐ€ํ•˜๊ธฐ : append()

append()๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋์—์„œ ์ƒˆ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€์‹œํ‚จ๋‹ค.

>>> fruit = ['apple', 'banana', 'mango']
>>> fruit.append('pine apple')
>>> fruit
fruit = ['apple', 'banana', 'mango', 'pine apple']

๋ฆฌ์ŠคํŠธ ๋ณ‘ํ•ฉํ•˜๊ธฐ : extend() ๋˜๋Š” +=

์ œ๋ชฉ์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณ‘ํ•ฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์šฐ๋ฆฌ๊ฐ€ ๋ฐ”๋กœ ์ „์— ์‚ฌ์šฉํ–ˆ๋˜ ๊ฒƒ์€ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ์ด๋‹ค. ์ด๋ฅผ ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ‘ํ•ฉํ•ด๋ณด์ž!

 

 

1. extend()ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ fruit์™€ flower๋ณ‘ํ•ฉํ•˜๊ธฐ

>>> flower.extend(fruit)
>>> flower
['rose', 'dongbaek', 'sun flower', 'apple', 'banana', 'mango']

2. += ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ flower๊ณผ human๋ณ‘ํ•ฉํ•˜๊ธฐ

>>> flower += human
>>> flower
['rose', 'dongbaek', 'sun flower', 'apple', 'banana', 'mango', 'jang', 'kim', 'hong']

์˜คํ”„์…‹๊ณผ insert()๋กœ ํ•ญ๋ชฉ ์ถ”๊ฐ€ํ•˜๊ธฐ

์œ„์— ์„ค๋ช…ํ–ˆ๋˜ append()๋Š” ๋‹จ์ง€ ๋ฆฌ์ŠคํŠธ์˜ ๋์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. ํ•˜์ง€๋งŒ insert() ํ•จ์ˆ˜๋Š” ์›ํ•˜๋Š” ์œ„์น˜์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜คํ”„์…‹ 0 ์€ ์‹œ์ž‘ ์ง€์ ์— ํ•ญ๋ชฉ์„ ์‚ฝ์ž…ํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋์„ ๋„˜๋Š” ์˜คํ”„์…‹์€ append() ์ฒ˜๋Ÿผ ๋์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€์‹œํ‚ค๋Š”๊ฒŒ ์ข‹๋‹ค.

>>> flower.insert(1, 'tree')
>>> flower
['rose', 'tree', 'dongbaek', 'sun flower']

์˜คํ”„์…‹์œผ๋กœ ํ•ญ๋ชฉ ์‚ญ์ œํ•˜๊ธฐ : del

 

์˜คํ”„์…‹์œผ๋กœ ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•  ๋•Œ๋Š” del์„ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๋ฐ ํ•œ ๊ฐ€์ง€ ์ฃผ์˜ํ•ด์•ผํ•  ์ ์ด ์žˆ๋‹ค. del()์€ ๋ฆฌ์ŠคํŠธ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ํŒŒ์ด์ฌ์˜ ๊ตฌ๋ฌธ์ธ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ flower.del(1)์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค

 

>>> del flower(1)
>>> flower
['rose', 'sun flower']

์˜คํ”„์…‹์œผ๋กœ ํ•ญ๋ชฉ์„ ์–ป์€ ํ›„ ์‚ญ์ œํ•˜๊ธฐ : pop()

 

pop()์€ ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ญ๋ชฉ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋™์‹œ์— ์‚ญ์ œํ•œ๋‹ค.

  • ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ : ํŠน์ • ๋งค๊ฐœ๋ณ€์ˆ˜(์ „๋‹ฌ์ธ์ž)์˜ ์˜คํ”„์…‹์œผ๋กœ ๊ฐ€์„œ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ ํ›„ ์‚ญ์ œ
  • ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†์„ ๋•Œ : -1๋ฒˆ์งธ ์˜คํ”„์…‹, ๋งˆ์ง€๋ง‰ ์˜คํ”„์…‹์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ ๋’ค ์‚ญ์ œ

 

>>> flower.pop(1)
['dongbaek']
>>> flower
['rose', 'sun flower']
>>> flower.pop()
['sun flower']
>>> flower
['rose']

๊ฐ’์œผ๋กœ ํ•ญ๋ชฉ ์˜คํ”„์…‹ ์ฐพ๊ธฐ : index()

 

ํ•ญ๋ชฉ ๊ฐ’์œผ๋กœ ๋ฆฌ์ŠคํŠธ์˜ ์˜คํ”„์…‹์„ ์•Œ๊ณ ์‹ถ๋‹ค๋ฉด index(),์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

>> flower.index('rose')
0

์˜คํ”„์…‹์˜ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ: in

 

๋ฆฌ์ŠคํŠธ์—์„œ ์–ด๋–ค ๊ฐ’์˜ ์กด์žฌ๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” in์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

>> 'rose' in flower
True
>> 'hae' in floser
False

 

๊ฐ’ ์„ธ๊ธฐ : count()

 

๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ • ๊ฐ’์ด ์–ผ๋งˆ๋‚˜ ์žˆ๋Š”์ง€ ์„ธ๊ธฐ ์œ„ํ•ด์„œ๋Š” count()๋ฅผ ์ด์šฉํ•œ๋‹ค.

 

>> student = ['jang', 'jung', 'hong', 'kim', 'jang', 'son']
>> student.count('jang')
3

๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ, ๋ฆฌ์ŠคํŠธ ์›์†Œ ํ•ฉ์น˜๊ธฐ : join()

 

์›์†Œ๋ฅผ ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋กœ ํ•ฉ์น˜๊ธฐ ์œ„ํ•ด์„œ ์ฆ‰, ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” join()์„ ์ด์šฉํ•œ๋‹ค.

 

 

>> person = ['jang', 'kim', 'son']
>> person
['jang', 'kim', 'son']
>> person_party = ','.join(person)
>> person_party
['jang, kim, son']

์ •๋ ฌํ•˜๊ธฐ : sort() ๋ฐ ํ•ญ๋ชฉ ๊ฐœ์ˆ˜ ์–ป๊ธฐ : len()

 

์ •๋ ฌํ•˜๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ํ•ญ๋ชฉ์ด ์ˆซ์ž์ธ ๊ฒฝ์šฐ ์˜ค๋ฆ„์ฐจ์ˆœ, ์•ŒํŒŒ๋ฒณ์ธ ๊ฒฝ์šฐ ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค.
๊ธฐ๋ณธ ์ •๋ ฌ์€ ์˜ค๋ฆ„์ฐจ์ˆœ์ด์ง€๋งŒ sort()์˜ ์ธ์ž ๊ฐ’์œผ๋กœ reverse=True๋ฅผ ์ฃผ๋ฉด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค.

 

 

>> letter = ['c', 'a', 'b', 'd']
>> letter.sort()
>> letter
['a', 'b', 'c', 'd']

ํ• ๋‹น : =, ๋ณต์‚ฌ : copy()

๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น๊ณผ ๋ณต์‚ฌ์— ๋Œ€ํ•ด์„œ ์ž์„ธํ•œ ์„ค๋ช…์ด ํ•„์š”ํ•œ๋ฐ ์‹ค์ œ ์˜ˆ๋ฅผ ํ†ตํ•ด์„œ ์•Œ์•„๋ณด์ž.

 

 

ํ• ๋‹น

>> a = ['a', 'b']

์ด ์ƒํƒœ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์–ด๋”˜๊ฐ€์— a๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ–๊ณ  ์žˆ๋Š” ์ฃผ์†Œ์—['a','b']๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ๋‹ค.

 

 

์ด ์ƒํƒœ์—์„œ

>> a = ['a', 'b']
>> b = a

 

๋ผ๊ณ  ํ•ด์ฃผ๋ฉด ์ง€๊ธˆ b๋Š” ์‹ค์ œ์ ์ธ ['a', 'b']๊ฐ€ ์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ a์˜ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐ ์ฆ‰, ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๋Š” ์ƒํƒœ๋ผ a๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด b๋„ ํ•จ๊ป˜ ๋ณ€๊ฒฝ๋œ๋‹ค ๊ทธ๋Ÿผ a๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ค๊ณ  b๋ฅผ ์ถœ๋ ฅ์‹œํ‚ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.

 

>> a = ['a', 'b']
>> b = a
>> a[0] = 'c'
>> a
>> ['c', 'b']
>> b
>> ['c', 'b']

๋ณต์‚ฌ

 

ํ•˜์ง€๋งŒ ๋ณต์‚ฌ๋Š” ๋‹ค๋ฅด๋‹ค.

 

>> a = ['a', 'b']
>> b = copy(a)

 

์ด์™€ ๊ฐ™์€ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ฒŒ ๋œ๋‹ค๋ฉด ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ['a', 'b']๋ผ๋Š” ๊ฐ’์ด ๋ณต์‚ฌ๋˜์–ด ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— a ๋ณ€๊ฒฝ์˜ ์œ ๋ฌด์™€ ์ƒ๊ด€ ์—†์ด ๊ฐ’์ด ์œ ์ง€๋œ๋‹ค.

๋Œ“๊ธ€