๐Ÿฅ Python 02: ์ž๋ฃŒ๊ตฌ์กฐ

yeeun leeยท2020๋…„ 3์›” 25์ผ
0

Python Basic

๋ชฉ๋ก ๋ณด๊ธฐ
3/6

์ž๋ฃŒ๊ตฌ์กฐ(data structure)๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ํ˜•ํƒœ๋กœ ์ €์žฅํ•ด ํŽธ๋ฆฌํ•˜๊ฒŒ ์ ‘๊ทผ ๋ฐ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค. ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋ณธ์งˆ ๋ฐ ์žฅ๋‹จ์ ์„ ์ดํ•ดํ•ด์•ผ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์ž˜ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ quote์ด ์žˆ์„ ์ •๋„๋กœ(feat. linus torvals) ์ž๋ฃŒ ๊ตฌ์กฐ๋Š” ์ค‘์š”ํ•˜๋‹ค.

Algorithms + Data Structures = Programs

Code is just the way to express the algorithms and the data structures.

0. ์ž๋ฃŒ ๊ตฌ์กฐ์˜ ๋ถ„๋ฅ˜

  • Primitive Data Structure(๋‹จ์ˆœ ๊ตฌ์กฐ) : ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…
  • None-Primitive Data Structure(๋น„๋‹จ์ˆœ ๊ตฌ์กฐ) : ๋‹จ์ˆœํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ตฌ์กฐ๊ฐ€ ์•„๋‹ˆ๋ผย ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชฉ์ ์— ๋งž๊ฒŒ ํšจ๊ณผ์ ์œผ๋กœ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ
    • Linear Data Structure(์„ ํ˜• ๊ตฌ์กฐ)
      : ์ €์žฅ๋˜๋Š” ์ž๋ฃŒ์˜ ์ „ํ›„ ๊ด€๊ณ„๊ฐ€ 1:1 (ex. List, Stacks, Queues)
    • Non-Linear Data Structure(๋น„์„ ํ˜• ๊ตฌ์กฐ)
      : ๋ฐ์ดํ„ฐ ํ•ญ๋ชฉ ์‚ฌ์ด์˜ ๊ด€๊ณ„๊ฐ€ 1:n ๋˜๋Š” n:m (ex. Graphs, Trees )

1. list []

๊ฐ’์ด ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์„ ๋•Œ, ์ผ์ผํžˆ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์€ ๋ฒˆ๊ฑฐ๋กœ์šธ ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ๊ฐ’์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅํ•ด index๋กœ value๋ฅผ ๊ตฌ๋ถ„ํ•˜๋ฉด ๋” ์ง๊ด€์ ์ด๊ธฐ ๋•Œ๋ฌธ์— list๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

1.1 ๊ธฐ๋ณธ ์šฉ์–ด

  • element: ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฐ’๋“ค์„ ์š”์†Œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. (html์˜ ์š”์†Œ<tag>content<tag>๊ฐ€ ์ƒ๊ฐ๋‚œ๋‹ค๐Ÿค”)
  • index: ์š”์†Œ๋“ค์˜ ์ˆœ์„œ. ์ฒซ ๋ฒˆ์งธ index๋Š” 0 ๋งˆ์ง€๋ง‰ index๋Š” list์˜ ํฌ๊ธฐ์ธ n-1์ด๋‹ค.
  • ์š”์†Œ๋“ค์€ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค.
  • ๋Œ€๊ด„ํ˜ธ[]๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋„ ์‚ฌ์šฉํ•˜๊ณ , ์š”์†Œ๋ฅผ ์ฝ์–ด๋“ค์ผ ๋•Œ๋„ ์‚ฌ์šฉํ•œ๋‹ค.

list ์•ˆ์˜ ์š”์†Œ๋ฅผ ๋ฐ”๊พธ๋Š” ๋ฐฉ์‹์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ํ—ท๊ฐˆ๋ฆฌ์ง€ ๋ง์ž.

1.2 method

- append

๋ฆฌ์ŠคํŠธ์˜ ๋์— ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ๋ง๋ถ™์ด๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋‹จ์ˆœํžˆ ์ถ”๊ฐ€ํ•˜๋Š” add๊ฐ€ ์•„๋‹Œ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•ด ๋ง๋ถ™์ด๋Š” append๋ผ๋Š” ์ ์„ ๊ธฐ์–ตํ•˜์ž.

color_list = ["Red", "Blue", "Green", "Black"]
color_list.append("Yellow") # ๋’ค์— ๋ถ™์—ฌ์ค˜! 

print(color_list) # result: ['Red', 'Blue', 'Green', 'Black', 'Yellow']

my_list=[]
my_list.append(1)
my_list = my_list + [2,3]

print(my_list)
# result: [1,2,3]

ํ”Œ๋Ÿฌ์Šค ๋ถ€ํ˜ธ (+)

์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•˜๋Š” ์š”์†Œ๊ฐ€ ํ•œ ๊ฐœ ์ด์ƒ์ผ ๋•Œ +๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ํ•ฉํ•œ ๊ฐ’์„ ์ €์žฅํ•˜๋ ค๋Š” ๋ณ€์ˆ˜์— ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. ํ•ฉํ•œ ๊ฐ’์„ ์“ฐ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‘๊ฐœ๋ฅผ ๋”ํ•œ ๊ฐ’์„ ๊ฐ์ฒด์— ํ• ๋‹นํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

# ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ ํ•  ๋•Œ๋Š” ์•„๋ž˜ ๋ฐฉ์‹์œผ๋กœ 
list1 = list1 + [ ์ถ”๊ฐ€ํ•˜๋Š” ์š”์†Œ ]  
list1 = list1 + list2 

#์˜ˆ์‹œ
color_list = ["black"]
color_list = color_list + ["Light Blue", "Pink"]'
print(color_list) # result: ["black", "Light Blue", "Pink"]

- insert

์›ํ•˜๋Š” ์œ„์น˜์— element๋ฅผ ๋„ฃ๊ณ  ์‹ถ์„ ๋•Œ ์“ด๋‹ค. ์ง€์ •ํ•œ ์œ„์น˜(num)์— ๋ผ์šฐ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๋’ค์— ์žˆ๋Š” ์š”์†Œ๋“ค์€ ๋’ค๋กœ ๋ฐ€๋ฆฐ๋‹ค.

๐Ÿšจ๋ฐ”๋กœ ์˜†์— ์žˆ๋Š” ์š”์†Œ๊ฐ€ ๋ฐ€๋ฆฌ๋Š”๊ฑฐ์ง€ ์—†์–ด์ง€๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค!

cities = [
    "์„œ์šธํŠน๋ณ„์‹œ",
    "๋ถ€์‚ฐ๊ด‘์—ญ์‹œ",
    "์ธ์ฒœ๊ด‘์—ญ์‹œ",
    "๋Œ€๊ตฌ๊ด‘์—ญ์‹œ",
    "๋Œ€์ „๊ด‘์—ญ์‹œ",
]
cities.insert(1, "์ œ์ฃผํŠน๋ณ„์ž์น˜๋„ ์ œ์ฃผ์‹œ") # index 1์— ์ œ์ฃผ์‹œ ๋‚‘๊ฒจ๋ณด์ž

print(cities)
''' result: cities = [
    "์„œ์šธํŠน๋ณ„์‹œ",
    "์ œ์ฃผํŠน๋ณ„์ž์น˜๋„ ์ œ์ฃผ์‹œ"
    "๋ถ€์‚ฐ๊ด‘์—ญ์‹œ",
    "์ธ์ฒœ๊ด‘์—ญ์‹œ",
    "๋Œ€๊ตฌ๊ด‘์—ญ์‹œ",
    "๋Œ€์ „๊ด‘์—ญ์‹œ",
]'''

- update elements

insert๋Š” ๋ฆฌ์ŠคํŠธ์— ๋ณ€ํ™” ์—†์ด ์ง€์ •ํ•œ ์œ„์น˜์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๊ณ , update๋Š” ๊ฐ’ ์ž์ฒด๋ฅผ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด๋‹ค. ๋ฐฉ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ธ๋ฑ์Šค์— value๋ฅผ assign ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

cities = [
    "์„œ์šธํŠน๋ณ„์‹œ",
    "๋ถ€์‚ฐ๊ด‘์—ญ์‹œ",
    "์ธ์ฒœ๊ด‘์—ญ์‹œ",
    "๋Œ€๊ตฌ๊ด‘์—ญ์‹œ",
    "๋Œ€์ „๊ด‘์—ญ์‹œ",
]
cities[2] = "๊ฒฝ๊ธฐ๋„ ์„ฑ๋‚จ์‹œ"

print(cities)
''' result: cities = [
    "์„œ์šธํŠน๋ณ„์‹œ",
    "๊ฒฝ๊ธฐ๋„ ์„ฑ๋‚จ์‹œ",
    "์ธ์ฒœ๊ด‘์—ญ์‹œ",
    "๋Œ€๊ตฌ๊ด‘์—ญ์‹œ",
    "๋Œ€์ „๊ด‘์—ญ์‹œ",
]'''

๋งŒ์•ฝ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์žˆ๋Š” ์š”์†Œ ๋‘ ๊ฐœ๋ฅผ ์„œ๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๊ผญ ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ์ผ ํ•„์š”๋Š” ์—†๋Š” ์ ์„ ์ฐธ๊ณ ํ•˜์ž.

(๊ทผ๋ฐ ์™œ ๊ฐœ๋ฐœ์ž๋“ค์€ ๋ณ€์ˆ˜๋‚˜ ๋ฆฌ์ŠคํŠธ ์ด๋ฆ„์„ foo ๋กœ ๋ถ™์ด๋Š”๊ฑธ ์ข‹์•„ํ• ๊นŒ..?)

foo[i], foo[j] = foo[j], foo[i]

- remove

del ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ ๋ถˆํŽธํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ. ํ‘œํ˜„์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋ฆฌ์ŠคํŠธ์—์„œ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋ฉด ๋’ค์— ์žˆ๋˜ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๊ฐ€ ์ž๋™์œผ๋กœ ์ •๋ ฌ๋˜๋ฏ€๋กœ index๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์— ์œ ์˜ํ•˜์ž!

twice = [ "๋‚˜์—ฐ",  "์‚ฌ๋‚˜",  "์ •์—ฐ",  "๋ชจ๋ชจ",  "๋ฏธ๋‚˜", "์ฑ„์˜",  "๋‹คํ˜„", "์ฏ”์œ„",  "์ง€ํšจ"]

del twice[3] # ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ
twice.remove("๋ชจ๋ชจ") # method ์‚ฌ์šฉ

1.3 del list ๊ณผ์ œ ๊ด€๋ จ

Python deleting elements from list ๋ฅผ ํ’€๋‹ค๊ฐ€ ์ฒ˜์Œ ๋ณด๋Š” ํ‘œํ˜„์‹์„ ๋ฐœ๊ฒฌํ–ˆ๋‹ค. ์ •๋ง ๊ฒ€์ƒ‰์˜ ํž˜(=๊ตฌ๊ธ€์˜ ํž˜)์ด๋ž€ ๋Œ€๋‹จํ•˜๋‹ค...

๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ ๋ฆฌ์ŠคํŠธ์—์„œ ํ™€์ˆ˜์ธ ์š”์†Œ๋งŒ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. ์š”์†Œ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜๋Š” for๋ฌธ์„ ์จ์„œ ์ง€์›Œ๋ณด๋ ค๊ณ  ํ–ˆ๋‹ค. ๊ทผ๋ฐ ๋‚˜๋ž‘ ๋˜‘๊ฐ™์€ ์‚ฌ๋žŒ์„ ์ฐพ์•„์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค ๐Ÿ˜ญ 7๋…„์ „์— ๋ฌผ์–ด๋ณธ ์‚ฌ๋žŒ์•„ ๊ณ ๋งˆ์›Œ...

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ณ„์† ํ™€์ˆ˜๊ฐ€ ์ง€์›Œ์ง€์ง€ ์•Š๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค! ๋ถ„๋ช… ๋ฌธ๋ฒ•์ ์œผ๋กœ๋Š” ๋งž๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ! ์ด ์‚ฌ์šฉ์ž๋„ ์–ด๋–ค ๊ฒƒ์€ ์ง€์›Œ์ง€๊ณ , ์–ด๋–ค ๊ฒƒ์€ ์•ˆ ์ง€์›Œ์ง€๋Š” ๋“ฑ ๋งค์šฐ ํ˜ผ๋ž€ํ–ˆ๋‹ค.

def remove_odd_numbers(numbers):
    for i in numbers:
        if i % 2 != 0:
            numbers.remove(i)
    return numbers

- del list ํ•ด๋‹ต

๊ฒŒ์‹œ๋ฌผ์˜ ๋Œ“๊ธ€์„ ๋ณด๋ฉด ๋ฐ˜๋ณตํ•  ๋•Œ ์•„์ดํ…œ์„ ์ง€์šฐ์ง€๋งˆ. ๊ทธ๊ฑด ๋‹ฌ๋ฆด ๋•Œ ๋ฐ”ํ€ด ๋นผ๋Š” ๊ฑฐ๋ž‘ ๋˜‘๊ฐ™์•„ ๋ผ๊ณ  ๋งค์šฐ ๊ทน๋‹จ์ ์ธ ์˜ˆ์‹œ๋ฅผ ๋“  ์‚ฌ๋žŒ๋„ ์žˆ์—ˆ๋‹ค. ์™œ๋ƒ๋ฉด ๋ฐ˜๋ณต์„ ํ•˜๋ฉด์„œ ์ง€์šฐ๊ฒŒ ๋˜๋ฉด, ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ ํฌ๊ธฐ๊ฐ€ ๋ฐ”๋€Œ๊ณ , ์ธ๋ฑ์Šค๋„ ๋ฐ”๋€Œ์–ด์„œ ๋ฐ˜๋ณตํ•˜๋‹ค๊ฐ€ ์š”์†Œ๋ฅผ ๊ฑด๋„ˆ ๋›ฐ๊ฒŒ ๋œ๋‹ค!! (๊ทธ๋Ÿผ ๊ทธ๋ ‡๋‹ค๊ณ  ๋ง์„ ํ•ด์•ผ์ง€ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์•ผ ๐Ÿ˜ ๐Ÿ˜ ๐Ÿ˜ )

๊ทธ๋ž˜์„œ ๋งŒ์•ฝ์— ๋ฆฌ์ŠคํŠธ์—์„œ for๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด shallow copy๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ๋งํ•˜๊ณ  ์žˆ๋‹ค. ์‚ฌ์‹ค ๋‚˜๋„ new_list=[]๋ฅผ ๋งŒ๋“  ๋’ค์— ๊ฑฐ๊ธฐ์— ์ง์ˆ˜๋“ค์„ append ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ–ˆ๋Š”๋ฐ, ์กฐ๊ฑด์— ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด ์•ˆ ๋˜์–ด์„œ ๊ณ„์† ์˜ค๋‹ต์ด ๋–ด๋‹ค.

์ •๋‹ต 1๋ฒˆ

def remove_odd(1):
    for i in l[:]: # l[:] ์€ list l์˜ shallow copy์ด๋‹ค. 
        if i % 2 != 0:
            l.remove(i)
    return l

์ •๋‹ต 2๋ฒˆ

๋˜๋Š” list comprehension์„ ์‚ฌ์šฉํ•ด ์ง์ˆ˜๋งŒ ๋‚จ๊ธธ ์ˆ˜๋„ ์žˆ๋‹ค. ์™€ ์ง„์งœ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด... ๋„ˆ๋ฌด ์ข‹์•„

def remove_odd_numbers(numbers):
  return [x for x in numbers if x % 2 == 0]

- list comprehension

๋ฆฌ์ŠคํŠธ๋ฅผ ์‰ฝ๊ฒŒ, ์งง๊ฒŒ ํ•œ ์ค„๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ํŒŒ์ด์ฌ์˜ ๋ฌธ๋ฒ•์ด๋‹ค. ๊ด€๋ จ ๊ฐœ๋…์„ ์ž˜ ์ •๋ฆฌํ•œ ๋ธ”๋กœ๊ทธ๊ฐ€ ์žˆ์–ด ๋‚ด์šฉ์„ ๊ฐ€์ ธ์™”์œผ๋ฉฐ, ๋งํฌ๋„ ์ฒจ๋ถ€ํ•œ๋‹ค.
โค๏ธ์ง„์งœ ๋„ˆ ๋ฌด ๊ฐ„ ๋‹จ ํ•˜ ๊ณ  ์•„ ๋ฆ„ ๋‹ต ๋‹ค . . . ๐Ÿฅบโค๏ธ

  • ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ๊ฐ’: ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•ด์„œ ์–ป๊ณ  ์‹ถ์€ value i*2
  • ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ ์ด๋ฆ„: ๋ณ€์ˆ˜ ์ •์˜ i
  • ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’: for๋ฌธ์—์„œ ์“ธ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  iterable ๊ฐ’. list, set, dict, tuple, range ๋“ฑ ํ•˜๋‚˜์”ฉ ๋Œ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ.
[ ( ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ๊ฐ’ ) for ( ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ ์ด๋ฆ„ ) in ( ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’ )]

size = 10
arr = [i * 2 for i in range(size)]

print(arr)

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

๐Ÿ”‡ filter ํ•จ์ˆ˜

filter ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋ผ๋Š” ๋Œ“๊ธ€๋„ ์žˆ์—ˆ๋‹ค. filter ํ•จ์ˆ˜๋Š” built-in ํ•จ์ˆ˜๋กœ list ๋‚˜ dictionary ๊ฐ™์€ iterable ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” ๊ฐ’๋งŒ ์ถ”์ถœํ•ด ๋‚ผ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

์ง€๊ธˆ์€ ๋„ˆ๋ฌด ํž˜์— ๋ถ€์น˜๋ฏ€๋กœ ์งฌ์ด ๋‚  ๋•Œ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ•˜์ž ๐Ÿ˜‚

1.4 slicing

๋ฆฌ์ŠคํŠธ์˜ ์ผ๋ถ€๋ถ„์„ copyํ•˜๋Š” ๊ฒƒ์„ slicing์ด๋ผ๊ณ  ํ•œ๋‹ค. start์™€ stop์€ index์˜ ์–ด๋””์„œ๋ถ€ํ„ฐ ์–ด๋””๊นŒ์ง€ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ธ์ง€, step์€ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๋ช‡ ์นธ์”ฉ ๊ฑด๋„ˆ๋›ธ ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ด๋‹ค. ๋งŒ์•ฝ 2๋ฉด 2์นธ์”ฉ ๋„์›Œ์„œ, -1์ด๋ฉด ์ˆœ์„œ๋ฅผ ๊ฑฐ๊พธ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.

๐Ÿšจstop์€ index+1์„ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค!

list_name[start : stop : step(optional)]
list_name[:] ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€
list_name[1:] index 1๋ถ€ํ„ฐ ๋๊นŒ์ง€
list_name[:5] ์ฒ˜์Œ๋ถ€ํ„ฐ index 5๊นŒ์ง€

๋งŒ์•ฝ ๊ธธ์ด๋ฅผ ๋ชจ๋ฅด๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ค‘๊ฐ„๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์œผ๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฆฌ์ŠคํŠธ/2์˜ ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š” ์ ์„ ์žŠ์ง€ ๋ง์ž.

my_list = [s for s in input().split()]
middle=int(len(my_list)/2) # ๋‚˜๋ˆˆ ๊ฐ’์€ ์ •์ˆ˜์—ฌ์•ผ slicing index๋กœ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.
print(my_list[:middle])
print(my_list[middle:])
print(my_list[1:5])

2. tuple ()

tuple์€ 2-5๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ‰์„์œผ๋กœ(ad hoc) ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

2.1 ์ƒ์„ฑ

์ผ๋ฐ˜ ๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ œ์™ธํ•˜๊ณ  ์ฝ์–ด๋“ค์ด๋Š” ๋ฐฉ๋ฒ•, slicing ๋“ฑ ๋ชจ๋‘ list์™€ ๋™์ผํ•˜๋‹ค.

my_tuple = (1, 2, 3)
my_tuple[0]
my_tuple[1:2]

2.2 ์˜ˆ์‹œ

tuple์€ ์ฃผ๋กœ list๊ณผ ํ•จ๊ป˜ ์“ฐ์ธ๋‹ค. list์˜ ์š”์†Œ๋กœ tuple์„ ์“ฐ๋Š”๋ฐ, A,B,C,D ์ขŒํ‘œ๋ฅผ ํ‘œํ˜„ํ•œ ๊ฒƒ์„ ์˜ˆ๋กœ ๋“ค์–ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

A = (4, 4)
B = (5, 2)
C = (-2, 2)
D = (-3, -4)
coords = [ (4,4),  (5,2),  (-2, 2),  (-3, -4) ]

list๋Š” ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๊ณ (๋” ๋งŽ์€ ๊ธฐ๋Šฅ, flexibleํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ) ๋ณดํ†ต ๋งŽ์€ ์–‘์„ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉํ•ด tuple๋ณด๋‹ค ์ฐจ์ง€ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰์ด ํฌ๋‹ค.

๋•Œ๋ฌธ์— ์ˆ˜์ •์ด ํ•„์š” ์—†๊ณ  ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ๋Š” tuple์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํšจ๊ณผ์ ์ด๋‹ค.

3. set

set์€ list์˜ ์นœ์ฒ™์ด๊ณ , ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ list๋ฅผ set์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค๋ฅธ ์ ์ด ์กด์žฌํ•œ๋‹ค.

  • ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋•Œ๋ฌธ์— for๋ฌธ์„ ๋Œ๋ฆฌ๋ฉด ๋ฌด์ž‘์œ„๋กœ ๋‚˜์˜จ๋‹ค.
  • idex๊ฐ€ ์—†๋‹ค.
  • ์ค‘๋ณต๋œ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์—†๋‹ค. ๋งŒ์ผ ์‹œ๋„ํ•˜๋ฉด ์ƒˆ๋กœ์šด ์š”์†Œ๊ฐ€ ์ด์ „ ์š”์†Œ๋ฅผ replace ํ•œ๋‹ค.

3.1 ์ƒ์„ฑ

2 ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  • ์ค‘๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉ
  • set() ํ•จ์ˆ˜๋กœ ๊ฐ์‹ธ์ฃผ๊ธฐ: list๋ฅผ parameter๋กœ ์ „๋‹ฌํ•œ๋‹ค.
set1 = {1, 2, 3}
set2 = set([1, 2, 3])

#๋งŒ์•ฝ ๋‹ค์‹œ list๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด?
list_again = list(set2)

3.2 ๋ณ€๊ฒฝ

- add()

set์€ ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— append๊ฐ€ ์•„๋‹ˆ๋ผ add๋ฅผ ์“ด๋‹ค.

my_set = {1, 2, 3}
my_set.add(4)
print(my_set)
> {1, 2, 3, 4}

- remove()

my_set = {1, 2, 3}
my_set.remove(3)
print(my_set)
> {1, 2}

- lookup

set์— ์–ด๋–ค ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ์•Œ์•„๋ณด๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. look up ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” in ํ‚ค์›Œ๋“œ๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.

my_set = {1, 2, 3}

if 1 in my_set:
    print("1 is in the set")
> 1 is in the set

if 4 not in my_set:
    print("4 is not in the set")
> 4 is not in the set

- ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ

  • ๊ต์ง‘ํ•ฉ: & ํ‚ค์›Œ๋“œ ๋˜๋Š” intersection ํ•จ์ˆ˜ ์‚ฌ์šฉ
  • ํ•ฉ์ง‘ํ•ฉ: | ํ‚ค์›Œ๋“œ ๋˜๋Š” union ํ•จ์ˆ˜ ์‚ฌ์šฉ
set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8, 9}

print(set1 & set2) # result: {4, 5, 6}

print(set1.intersection(set2)) # result: {4, 5, 6}

print(set1 | set2) # result: {1, 2, 3, 4, 5, 6, 7, 8, 9}

print(set1.union(set2))  result: {1, 2, 3, 4, 5, 6, 7, 8, 9}

4. dictionary

dictionary๋Š” key์™€ value๋กœ ์ง์„ ์ง€์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” index๋กœ ๋ถ€๋ฅด์ง€๋งŒ, dictionary๋Š” key๋กœ ๋ถ€๋ฅด๊ฒŒ ๋œ๋‹ค.

4.1 ์ƒ์„ฑ

my_dic = { "key1" : "value1", "key2" : "value2"}
my_dic["key1"] # result: value1 

๐ŸŒˆkey์™€ ๊ด€๋ จ๋œ ๊ทœ์น™

  • data type์€ string๊ณผ number๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • key๊ฐ’์€ ์ค‘๋ณต๋  ์ˆ˜ ์—†๋‹ค. ๋งŒ์•ฝ ์ค‘๋ณต๋  ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด key๊ฐ’์ด ๊ธฐ์กด ๊ฐ’์„ replaceํ•˜๊ฒŒ ๋œ๋‹ค.

4.2 ํ™•์ธ ๋ฐ ๋ณ€๊ฒฝ

- ํ™•์ธ

๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์ ์šฉํ•˜๋Š” ํ•จ์ˆ˜. key ๊ฐ’๋งŒ ๋ฆฌ์ŠคํŠธ๋กœ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. key๊ฐ€ ์•„๋‹Œ keys์ด๊ณ , property๊ฐ€ ์•„๋‹Œ method์ธ ์ ์„ ๊ธฐ์–ตํ•˜์ž!

my_dict = 
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 'hi': 'greeting'}

my_dict.keys() 
# result: dict_keys([1, 2, 3, 4, 'hi'])

- ์ถ”๊ฐ€

๋ฆฌ์ŠคํŠธ์™€ ๋น„์Šทํ•˜์ง€๋งŒ index๊ฐ€ key๋กœ ๋Œ€์ฒด๋œ ๊ฐœ๋…์œผ๋กœ ๋ณด๋ฉด ๋œ๋‹ค. ์ถ”๊ฐ€ํ•˜๋ฉด ๋’ค์— ์ˆœ์„œ๋Œ€๋กœ ๋ถ™๋Š”๋‹ค (list์˜ append์ฒ˜๋Ÿผ)

dictionary_name[new_key] = new_value

- ์ˆ˜์ •

key์— value๋ฅผ ๋‹ค์‹œ ํ• ๋‹นํ•˜๋Š” ๊ฐœ๋…์œผ๋กœ ์ ‘๊ทผํ•œ๋‹ค.

my_dict[want_to_change_key] = value 

my_dict = { 1: "one", 2: "two", 3: "four" }
my_dict[3] = "three"
print(my_dict)
> {1:"one", 2: 'two', 3: "three"}

- ์‚ญ์ œ

my_dict = { 1: "one", 2: "two", 3: "three" }
del my_dict["one"]
print(my_dict)
> {2: 'two', 3: 'three'}

4.3 complex dictionary

๋”•์…”๋„ˆ๋ฆฌ์— ๋‚ด์šฉ์ด ์—„์ฒญ ๋งŽ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ?

- dict์˜ ์š”์†Œ๋ฅผ list๋กœ

๋”•์…”๋„ˆ๋ฆฌ๋ฅผ list์˜ ํ•œ ์š”์†Œ๋กœ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•. ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ทธ๋ฃจํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ด๋ฉฐ, for ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด ์ฝ์–ด๋“ค์ผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋‹ค ์ฝ์–ด์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋Œ๋ฆฌ๋Š” ๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค.

my_cat = [
   { 
   "name" : "captain",
   "age" : 13,
   "gender" : "male"
   },
   
   {
   "name" : "marble",
   "age" : 10,
   "gender" : "female"
   }
]

for cat in my_cat:
   if cat["name"] == "captain":
      print(cat["age"]) 
        
# result: 13
     

nested dict

dict ์•ˆ์— ์ค‘์ฒฉ์œผ๋กœ dict๋ฅผ ๋„ฃ์–ด index๋งŒ์œผ๋กœ ์›ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

my_cat = 
   {
   "captain" : { 
      "age" : 13,
      "gender" : "male"
   },
   
   {
   "marble" : {
      "age" : 10,
      "gender" : "female"
   }
}
print(my_cat["captain"]["age"]) # result :  13 

์ž๋ฃŒ ๊ตฌ์กฐ๋Š” ์ด์ •๋„๋กœ... ๋น ์ด ๐Ÿ–๐Ÿป

profile
์ด์‚ฌ๊ฐ„ ๋ธ”๋กœ๊ทธ: yenilee.github.io

0๊ฐœ์˜ ๋Œ“๊ธ€