๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ Algorithm/Python

[Python] ๋ฐ์ดํ„ฐ ํƒ€์ž… - List, Dictionary, JSON

by Danna 2020. 12. 7.
728x90
728x90

๋ฐ์ดํ„ฐ ํƒ€์ž…

immutable๊ณผ mutable ์€ ๋ณ€์ˆ˜๊ฐ€ ๋‹ด๊ณ  ์žˆ๋Š” object์˜ ์†์„ฑ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋Š” ํŠน์„ฑ์ด๋‹ค.

๋ณ€์ˆ˜๊ฐ€ ๋‹ด๊ณ ์žˆ๋Š” object์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์—†๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ immutable object ๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ๋ณ€์ˆ˜๊ฐ€ ๋‹ด๊ณ ์žˆ๋Š” object์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด mutable object ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

  • immutable object : int, float, bool, string, tuple
  • mutable object : list, dictionary

id(object) ํ•จ์ˆ˜๋Š” object์˜ ๊ณ ์œ ํ•œ ์ƒ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ๋ฆฌํ„ดํ•˜๋Š” ๊ฐ’์ด ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ์ƒ์˜ ์ฃผ์†Œ๋Š” ์•„๋‹ˆ์ง€๋งŒ, object๋งˆ๋‹ค ์กด์žฌํ•˜๋Š” ๊ณ ์œ ๊ฐ’์ด๋‹ค.

์•„๋ž˜ ์˜ˆ์ œ๋Š” immutable object, mutable object๋ฅผ ๋‹ด๊ณ ์žˆ๋Š” ๋ณ€์ˆ˜์— ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ ํ›„ id() ํ•จ์ˆ˜ ๊ฐ’์ด ๋‹ฌ๋ผ์ง€๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.

# immutble object - int, float, bool, string, tuple
>>> a = 1 # a๋Š” 1์ด๋ผ๋Š” int object๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.
>>> id(a)
29688152
>>> a += 1 # ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ, object 1์ด ๋ฐ”๋€Œ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ
>>> id(a)  # ๋‹ค๋ฅธ int object๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค.
29688128

>>> str = 'a'
>>> id(str)
140300455077128
>>> str = str + 'b' # str = 'ab', ๊ธฐ์กด๊ณผ ๋‹ค๋ฅธ object๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค.
>>> id(str) # ์—ฐ์‚ฐ์„ ํ•˜๊ธฐ ์ „๊ณผ id๊ฐ€ ๋‹ค๋ฅด๋‹ค.
140300454036544

>>> t = (1, 2)
>>> id(t)
140300453904680
>>> t = t + (3,) # t = (1, 2, 3)
>>> id(t) # ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „๊ณผ ๋‹ค๋ฅธ object๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
140300453980112

# tuple object์˜ ์›์†Œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•˜๋ฉด TypeError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค
>>> t[0] = 5
...
TypeError: 'tuple' object does not support item assignment
# mutable object - list, dictionary
>>> l = [1, 2]
>>> id(l)
140300453998672
>>> l.append(3) # l = [1, 2, 3]
>>> id(l) # ๊ธฐ์กด์˜ list object์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์–ด, id๊ฐ’์ด ๊ฐ™๋‹ค.
140300453998672

>>> d = {'key1': 'value1'}
>>> id(d)
140300454060392
>>> d['key2'] = 'value2' # d = {'key2':'value2', 'key1':'value1'}
>>> id(d) # ๊ธฐ์กด์˜ dictionary object์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์–ด, id๊ฐ’์ด ๊ฐ™๋‹ค.
140300454060392

List ๊ธฐ๋ณธ ํ•จ์ˆ˜

List object๋Š” ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ ํ•จ์ˆ˜๋ฅผ ์ •๋ฆฌํ•ด๋‘๋Š”๊ฒŒ ์ข‹๋‹ค.

list.append(x)

  • ์ธ์ž๋กœ ๋ฐ›์€ x์˜ ํ˜•ํƒœ ๊ทธ๋Œ€๋กœ ํ•ด๋‹น list ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  • ๋งค๊ฐœ๋ณ€์ˆ˜ x๋Š” ์ˆซ์ž, List, Tuple, Dictionary์˜ ํ˜•ํƒœ ์ „๋ถ€ ๊ฐ€๋Šฅํ•˜๋‹ค
  • ํ•จ์ˆ˜ ์‹คํ–‰์‹œ list ๊ฐ’์ด ๋ฐ”๋กœ ๋ณ€ํ™”๋œ๋‹ค.
  • Return ๊ฐ’ ์—†์Œ

list.extend(iterable)

  • ๋งค๊ฐœ๋ณ€์ˆ˜ iterble์ž๋ฆฌ์—๋Š” ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ object๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • list, tuple, string, dictionary ๋“ฑ
    • dictionary๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒฝ์šฐ key๊ฐ’๋งŒ ์—ฐ์žฅ๋œ๋‹ค.
  • ์ธ์ž๋กœ ๋ฐ›์€ object์˜ ๊ฐ’๋“ค์„ ํ•ด๋‹น list ๋งˆ์ง€๋ง‰์— ์—ฐ๊ฒฐํ•ด ์—ฐ์žฅํ•œ๋‹ค.
  • ํ•จ์ˆ˜ ์‹คํ–‰์‹œ list์˜ ๊ฐ’์ด ๋ฐ”๋กœ ๋ณ€ํ™”๋œ๋‹ค.
  • Return ๊ฐ’ ์—†์Œ
>>> list = [1, 2, 3]
>>> list.extend("abcd")
>>> list
[1, 2, 3, 'a', 'b', 'c', 'd']
>>> list.extend({'key1':'value1'})
>>> list
[1, 2, 3, 'a', 'b', 'c', 'd', 'key1']

List + List

  • extend()ํ•จ์ˆ˜์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•˜์ง€๋งŒ, List๋งŒ ์—ฐ์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ•จ์ˆ˜ ์‹คํ–‰์‹œ list์˜ ๊ฐ’์€ ๋ณ€ํ™”ํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • Return ๊ฐ’์œผ๋กœ๋Š” ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•ฉ์นœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

list.pop([index])

  • ์ธ์ž๋กœ ๋ฐ›์€ index์— ํ•ด๋‹นํ•˜๋Š” ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.
  • ๋งค๊ฐœ๋ณ€์ˆ˜ index๋Š” ์ •์ˆ˜๋กœ, ์ƒ๋žตํ•  ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๊ฐ’์ด ์‚ญ์ œ๋œ๋‹ค.
  • ์ธ์ž๋กœ list์˜ index๋ณด๋‹ค ํฐ ๊ฐ’์„ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ IndexError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. (์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ)
  • ํ•จ์ˆ˜ ์‹คํ–‰์‹œ list์˜ ๊ฐ’์ด ๋ฐ”๋กœ ๋ณ€ํ™”๋œ๋‹ค.
  • list์—์„œ ์‚ญ์ œ๋˜๋Š” ์•„์ดํ…œ ๊ฐ’์ด ๋ฐ˜ํ™˜๋œ๋‹ค.

list.remove(x)

  • ์ธ์ž๋กœ ๋ฐ›์€ x๊ฐ’๊ณผ ๊ฐ™์€ ์š”์†Œ ์ค‘ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.
  • ํ•จ์ˆ˜ ์‹คํ–‰์‹œ list์˜ ๊ฐ’์ด ๋ฐ”๋กœ ๋ณ€ํ™”๋œ๋‹ค.
  • Return ๊ฐ’ ์—†์Œ
  • ์ธ์ž๋กœ list์— ์—†๋Š” ๊ฐ’์„ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ ValueError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. (์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ)

list.insert(index, x)

  • ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ ๋ฐ›์€ index์— ์•„์ดํ…œ x ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.
  • ํ•จ์ˆ˜ ์‹คํ–‰์‹œ list์˜ ๊ฐ’์ด ๋ฐ”๋กœ ๋ณ€ํ™”๋œ๋‹ค.
  • index์ธ์ž์— list์˜ index๋ณด๋‹ค ํฐ ๊ฐ’์„ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ, ๋งจ ๋’ค์— ์‚ฝ์ž…๋œ๋‹ค.
  • Return ๊ฐ’ ์—†์Œ

List ๊ธฐ๋ณธํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ์˜ˆ์ œ

>>> a = [1, 2, 3]
>>> a.append(5) # a ๋’ค์— ์•„์ดํ…œ์ด ์ถ”๊ฐ€๋œ๋‹ค. a = [1, 2, 3, 5]
>>> a.append([2, 5]) # a = [1, 2, 3, 5, [2, 5]]
>>> a.pop() # ํŒ๋˜๋Š” ์•„์ดํ…œ์ธ [2, 5] ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค. ์ธ๋ฑ์Šค ์ง€์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ œ์ผ ๋งˆ์ง€๋ง‰ ๊ฐ’ ์‚ญ์ œ
[2, 5]
>>> a.pop(2) # index=2 ์•„์ดํ…œ์ธ 3 ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค. a = [1, 2, 5]
3
>>> a.extend([3, 4, 5]) # List [3, 4, 5] ๋ฅผ a์˜ ๋’ค์— ์—ฐ๊ฒฐํ•œ๋‹ค. a = [1, 2, 5, 3, 4, 5]

>>> a.remove(4) # 4 ์ธ ์•„์ดํ…œ์„ List์—์„œ ์‚ญ์ œํ•œ๋‹ค. a = [1, 2, 5, 3, 5]

>>> a.insert(2, 2) # List์˜ 2๋ฒˆ์งธ ์ธ๋ฑ์Šค์— 2๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค. a = [1, 2, 2, 5, 3, 5]

>>> a + [6, 7] # list a์™€ list [6, 7] ๋ฅผ ํ•ฉ์นœ๋‹ค. a = [1, 2, 2, 5, 3, 5]
[1, 2, 2, 5, 3, 5, 6, 7]

List ๋ณต์‚ฌ - shallow copy, deep copy

mutable object๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜๋ฅผ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜๋ฉด ๊ธฐ์กด object๋ฅผ ๊ณต์œ ํ•œ๋‹ค. ๊ฐ™์€ object๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜ ์˜ˆ์ œ์—์„œ list2์˜ ์š”์†Œ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋˜๋ฉด, list1์˜ ์š”์†Œ๊ฐ’๋„ ๋ณ€๊ฒฝ๋œ๋‹ค.

ํ•ด๋‹น ๊ฒฝ์šฐ๋Š” shallow copy(์–•์€ ๋ณต์‚ฌ)์— ์†ํ•œ๋‹ค.

>>> a, b = [1, 2], [3, 4]
>>> list1 = [a, b]
>>> list2 = list1 # list1, list2๊ฐ€ ๊ฐ™์€ object๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
>>> print list1, list2, list1 is list2
[[1, 2], [3, 4]] [[1, 2], [3, 4]] True

# list2๋Š” list1์€ ๊ฐ™์€ object๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฏ€๋กœ list1๋„ ๋ณ€ํ•œ๋‹ค.
# 10์ด๋ผ๋Š” int object๋กœ ์žฌํ• ๋‹น๋˜๊ธฐ ๋•Œ๋ฌธ์— a์˜ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.
>>> list2[0] = 10
>>> print list1, list2, list1 is list2
[10, [3, 4]] [10, [3, 4]] True
>>> print a, b
[1, 2] [3, 4]

# list1[1][0], list2[1][0] ์˜ ๊ฐ’์€ b[0]๊ณผ ๊ฐ™์€ ๊ฐ’์ด๋ฏ€๋กœ b์˜ list object๊ฐ€ ๋ณ€๊ฒฝ๋œ๋‹ค.
>>> list2[1][0] = 33
>>> print list1, list2, list1 is list2
[10, [33, 4]] [10, [33, 4]] True
>>> print a, b
[1, 2] [33, 4]

์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ list object๋ฅผ ๋ณต์‚ฌ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ shallow copy์— ํ•ด๋‹นํ•œ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ์—์„œ list1, list2๋Š” ๋‹ค๋ฅธ list object์ด์ง€๋งŒ, ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋“ค์€ a, b ์˜ list object๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค.

list1, list2๋Š” ๋‹ค๋ฅธ list object์ด๋ฏ€๋กœ list1[n] list2[n]์˜ ๊ฐ’์„ ์žฌํ• ๋‹น ํ•  ๊ฒฝ์šฐ, ์„œ๋กœ์˜ list์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.

list1, list2์˜ ์š”์†Œ๊ฐ’์ธ list1[0], list2[0]๋Š” a์˜ list object๋ฅผ ๊ณต์œ ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— list2[n][m] ๊ฐ’ ๋ณ€๊ฒฝ์‹œ, list1[n][m] ๊ฐ’ ๋˜ํ•œ ๋ณ€ํ•˜๊ฒŒ ๋œ๋‹ค.

  • copy module์˜ copy()ํ•จ์ˆ˜๋„ ์Šฌ๋ผ์ด์‹ฑ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.
>>> a, b = [1, 2], [3, 4]
>>> list1 = [a, b]
>>> list2 = list1[:]
>>> list1, list2, list1 is list2 # list1, list2๋Š” ๋‹ค๋ฅธ object
[[1, 2], [3, 4]] [[1, 2], [3, 4]] False

# list2์™€ list1์€ ๋‹ค๋ฅธ object์ด๋ฏ€๋กœ list1์€ ์˜ํ–ฅ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.
# list2[0]์„ 10์ด๋ผ๋Š” int object๋กœ ์žฌํ• ๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์— a์˜ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.
>>> list2[0] = 10
>>> print list1, list2, list1 is list2
[[1, 2], [3, 4]] [10, [3, 4]] False
>>> print a, b
[1, 2] [3, 4]

# List2[1]๊ณผ List1[1]์€ b๋ผ๋Š” list object๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฏ€๋กœ
# List2[1][0]์„ ๋ณ€๊ฒฝํ•˜๋ฉด List1[1][0]๋„ ๋ณ€๊ฒฝ๋˜๊ณ , b์˜ list object ๊ฐ’๋„ ๋ณ€๊ฒฝ๋œ๋‹ค.
>>> list2[1][0] = 33
>>> print list1, list2, list1 is list2
[[1, 2], [33, 4]] [10, [33, 4]] False
>>> print a, b
[1, 2] [33, 4]

list ๋ณต์‚ฌ ์‹œ copy ๋ชจ๋“ˆ์˜ deepcopy() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ณต์‚ฌ๋ฅผ ํ•˜๋ฉด ์ƒˆ๋กœ์šด list object๊ฐ€ ์ƒ์„ฑ๋  ๋ฟ ์•„๋‹ˆ๋ผ, ๋‚ด๋ถ€์˜ ์š”์†Œ๋“ค๋„ ์ƒˆ๋กœ์šด object๋กœ ์ƒ์„ฑ๋˜์–ด ๋‘ list object๋ฅผ ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> a, b = [1, 2], [3, 4]
>>> list1 = [a, b]
>>> list2 = list1[:]
>>> list1, list2, list1 is list2 # list1, list2๋Š” ๋‹ค๋ฅธ object
[[1, 2], [3, 4]] [[1, 2], [3, 4]] False

# list2๋ฅผ ๋ณ€๊ฒฝํ•ด๋„ list1์€ ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค.
>>> list2[0] = 10
>>> list1, list2, list1 is list2
[[1, 2], [3, 4]] [10, [3, 4]] False
>>> print a, b
[1, 2] [3, 4]

# list2๋ฅผ ๋ณ€๊ฒฝํ•ด๋„ list1์€ ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค.
# ๊ธฐ์กด๊ณผ ๋‹ฌ๋ฆฌ ๋‚ด๋ถ€์˜ object๋„ ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๋ฏ€๋กœ a, b์— ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค.
>>> list2[1][0] = 33
>>> list1, list2, list1 is list2
[[1, 2], [3, 4]] [10, [33, 4]] False
>>> print a, b
[1, 2] [3, 4]

Deep copy๊ฐ€ ์ง€์›๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋ณดํ†ต recursive(์žฌ๊ท€) ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” 2์ฐจ์› list์— ๋Œ€ํ•ด์„œ ์˜ˆ์‹œ๋ฅผ ์ƒ๊ฐํ•ด๋ณธ ์ฝ”๋“œ์ด๋‹ค.

list a[i]๋ฅผ list b[i]์— append ํ•  ๊ฒฝ์šฐ, shallow copy๊ฐ€ ๋˜๋ฏ€๋กœ list a[i]์˜ ์š”์†Œ ๊ฐ’์„ temp์— ์ €์žฅํ•œ ํ›„, list b[i]์— append ํ•ด์ค€๋‹ค. a๊ฐ€ 3์ฐจ์› ๋ฆฌ์ŠคํŠธ์ผ ๊ฒฝ์šฐ, temp๋ฅผ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด์ค˜์•ผํ•œ๋‹ค.

a = [[1, 2], [3, 4]]
b = []
>>> for i in range(len(a)):
...     temp = []
...     for j in range(len(a[i])):
...             temp.append(a[i][j])
...     b.append(temp)
...
>>> print a, b, a is b, a[0] is b[0], a[0][0] is b[0][0]
[[1, 2], [3, 4]] [[1, 2], [3, 4]] False False True
>>> b[0][0] = 5
>>> print a, b, a is b, a[0] is b[0], a[0][0] is b[0][0]
[[1, 2], [3, 4]] [[5, 2], [3, 4]] False False False

Dictionary object

dictionary ์ž๋ฃŒํ˜•์€ {key:value} ํ˜•ํƒœ๋ฅผ ๊ฐ–๋Š”๋‹ค. ์ˆœ์„œ๊ฐ€ ์—†๋Š” ์ž๋ฃŒํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹Œ key ๊ฐ’์œผ๋กœ ์ ‘๊ทผํ•ด value๊ฐ’์„ ์–ป๋Š”๋‹ค.

key ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์€ hashmap์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” hashable type ์ด์–ด์•ผ ํ•œ๋‹ค.

  • hashable object์ธ ์ˆซ์ž, ๋ฌธ์ž์—ด, tuple์€ key๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • tuple์˜ ๊ฒฝ์šฐ, list, dictionary๋ฅผ ์š”์†Œ๋กœ ๊ฐ–๊ณ ์žˆ๋Š” tuple์€ key๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
  • hashable ํ•˜์ง€ ์•Š์€ object์ธ list, dictionary๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ TypeError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

hashable object ๋Š” object์˜ ์ƒ๋ช…์ฃผ๊ธฐ ๋™์•ˆ ๋ณ€ํ•˜์ง€ ์•Š๋Š” hash ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. hash() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด hash ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. hashable object๋งŒ ๊ฐ€๋Šฅํ•˜๋ฉฐ, hashableํ•˜์ง€ ์•Š์€ object์˜ hash ๊ฐ’์„ ์–ป์œผ๋ ค๊ณ  ํ•  ๊ฒฝ์šฐ TypeError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋‘ object์˜ hash ๊ฐ’์„ ๊ฐ™์€์ง€ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” obj.__eq__(obj2) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. [dict type ๋ฌธ์„œ ๋งํฌ] [hashable ๋ฌธ์„œ ๋งํฌ]

value์—๋Š” ๋ชจ๋“  object๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • value๋Š” ์ค‘๋ณต๋  ์ˆ˜ ์žˆ์ง€๋งŒ, key๊ฐ€ ์ค‘๋ณต๋˜๋ฉด ๋งˆ์ง€๋ง‰์— ๋“ฑ๋กํ•œ ๊ฐ’์œผ๋กœ ๋ฎ์–ด์”Œ์›Œ์ง„๋‹ค.
  • value์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” d[key] = new_value ํ˜•์‹์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
# unhashable type์„ key๋กœ ์‚ฌ์šฉํ–ˆ์„ ๊ฒฝ์šฐ
>>> dict_test = {[1, 2, 3]: 1}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

>>> a, b, c = 1, 2, 3
>>> dict_test = {"a": a, "b": b} # ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
>>> dict_test["c"] = c # ์ƒˆ๋กœ์šด ํ‚ค์™€ ๊ฐ’์„ ์ถ”๊ฐ€
>>> dict_test
{'a': 1, 'c': 3, 'b': 2}

>>> dict_test = dict(a = a, b = b, c = c) # dict ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ƒ์„ฑ
>>> dict_test
{'a': 1, 'c': 3, 'b': 2}

>>> dict_test.keys() # dict ์˜ key๊ฐ’๋“ค์„ ์ƒˆ๋กœ์šด list๋กœ ๋ฐ˜ํ™˜
['a', 'c', 'b']
>>> dict_test.values() # dict ์˜ value๊ฐ’๋“ค์„ ์ƒˆ๋กœ์šด list๋กœ ๋ฐ˜ํ™˜
[1, 3, 2]

JSON

JSON(JavaScript Object Notation)์€ ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ํ˜•์‹์ด๋‹ค. ์›น ๋ธŒ๋ผ์šฐ์ €-์›น ์„œ๋ฒ„ ์‚ฌ์ด์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜ํ•˜๋Š”๋ฐ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํ˜•์‹์ด๋‹ค. ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์ด๊ธฐ๋•Œ๋ฌธ์— ์‚ฌ๋žŒ์ด ๋ณผ ๋•Œ์— ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฃผ๋กœ Key-value ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค. ์ด๋ฅผ JSON์—์„œ๋Š” object๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

JSON ํ‘œ๊ธฐ๋ฒ•

  • JSON object๋Š” {์ค‘๊ด„ํ˜ธ}๋กœ ํ‘œ๊ธฐํ•œ๋‹ค.
  • JSON array๋Š” [๋Œ€๊ด„ํ˜ธ]๋กœ ํ‘œ๊ธฐํ•œ๋‹ค.
  • Key(์†์„ฑ): Value(๊ฐ’) ํ˜•ํƒœ๋กœ ํ‘œ๊ธฐํ•œ๋‹ค.
    • Key(์†์„ฑ)์€ "์Œ๋”ฐ์˜ดํ‘œ"๋กœ ๋ฌถ์–ด์„œ ๋ฌธ์ž์—ด๋กœ ํ‘œ๊ธฐํ•œ๋‹ค. ์ˆซ์ž๋„ ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„๋œ๋‹ค.
    • Value(๊ฐ’)์€ ๋ชจ๋“  ์ž๋ฃŒํ˜•์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ ์ž๋ฃŒํ˜•์— ๋”ฐ๋ผ ํ‘œ๊ธฐ ๋ฐฉ๋ฒ•์ด ๋‹ฌ๋ผ์ง„๋‹ค.
  • ์†์„ฑ์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ , ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.
  • JSON์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์œ ๋‹ˆ์ฝ”๋“œ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•œ๋‹ค.
[
  {
    "name": "song",
    "age": 23,
    "isAgree": true,
    "hobby": null
  },
  {
    "name": "danna",
    "age": 23,
    "isAgree": false,
    "hobby": "yoga"
  }
]

python ์—์„œ JSON ๋ชจ๋“ˆ์€ Python์˜ object๋ฅผ ๋ฌธ์ž์—ด ํ‘œํ˜„์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ , ๋ฌธ์ž์—ด ํ‘œํ˜„์„ Python์˜ object๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค. ์ฃผ๋กœ key-value ๋ฐฉ์‹์˜ JSON๋ฌธ์ž์—ด๊ณผ dictionary ํƒ€์ž… ๊ฐ„์˜ ๋ณ€๊ฒฝ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

 

Python object์™€ JSON ์ž๋ฃŒํ˜• ๊ฐ„์˜ ๋ณ€ํ™˜

  • dict - object {key:value}
  • list, tuple - [array]
  • str - string
  • int, long, float - number
  • True, False - true, false
  • None - null

JSON ๋ฌธ์ž์—ด๋กœ ์ €์žฅ์‹œ [์ธ์ฝ”๋”ฉ]
json.dump(obj, fp) : ์ฒซ๋ฒˆ์งธ ์ธ์ž obj๋ฅผ JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ” ์˜คํ”ˆํ•œ file์— ์“ฐ๊ธฐ
json.dumps(obj) : file๋กœ ์“ฐ์ง€ ์•Š๊ณ , ํŒŒ์ด์ฌ ๋‚ด์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด json ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พผ string์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

JSON ํฌ๋งท์„ ๋กœ๋“œ์‹œ [๋””์ฝ”๋”ฉ]
json.load(fp) : json file์„ ๋กœ๋“œํ•ด python object๋กœ ๋ณ€ํ™˜ํ•ด ๊ทธ ๋ณ€ํ™˜ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
json.loads(string) : ๋ฌธ์ž์—ด์„ Python object๋กœ ๋ณ€ํ™˜ํ•ด ๊ทธ ๋ณ€ํ™˜ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

# python ์˜ˆ์ œ
>>> import json
>>> person = {
...     'id':1,
...     'name':'danna',
...     'school':[
...             {'year':2013, 'school':'sunrin'},
...             {'year':2016, 'shcool':'konkuk'},
...     ]
... }

>>> jsonString = json.dumps(person, indent=4)
>>> print type(jsonString)
<type 'str'>
>>> print jsonString
{
    "school": [
        {
            "school": "sunrin",
            "year": 2013
        },
        {
            "shcool": "konkuk",
            "year": 2016
        }
    ],
    "id": 1,
    "name": "danna"
}

2019.07.15 ๊ณต๋ถ€ ์ž๋ฃŒ ์—…๋กœ๋“œ

728x90
728x90