TIL25. Python : Comparison of Sequence Data Types

ID์งฑ์žฌยท2021๋…„ 10์›” 7์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
32/39
post-thumbnail

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” Python์˜ Set ๊ณผ Dictionary ์˜ ์ฐจ์ด, List ์™€ Tuple ์˜ ์ฐจ์ด์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.



๐ŸŒˆ Comparison of Sequence Data Types

๐Ÿ”ฅ Set ๊ณผ Dictionary์˜ ์ฐจ์ด

๐Ÿ”ฅ List ์™€ Tuple์˜ ์ฐจ์ด



1. Set ๊ณผ Dictionary์˜ ์ฐจ์ด

๐Ÿค” {}์„ ์“ฐ๋Š” ๋‘ ์นœ๊ตฌ

โœ”๏ธ dictํ˜•๊ณผ setํ˜• ๋ชจ๋‘ {}์œผ๋กœ ๊ฐ์‹ธ์ ธ ์žˆ์ง€๋งŒ, ๋‚ด๋ถ€๊ตฌ์กฐ๋Š” dictํ˜•์€ key์™€ value๋กœ ์ด๋ค„์ ธ์žˆ๊ณ , set์€ ์ค‘๋ณต๋˜์ง€ ์•Š์€ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ๋‹จ, dictํ˜•์€ key๋ฅผ ์ค‘๋ณต์‹œํ‚ฌ ์ˆ˜ ์—†์ง€๋งŒ value๋Š” ์ค‘๋ณต์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ dictํ˜•์€ ๊ตฌ์กฐ์ ์œผ๋กœ ๋‹ค์–‘ํ•œ ์ž๋ฃŒํ˜•์€ ๋ณด๊ด€ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ณ , set์€ ๊ต์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ ๋“ฑ์˜ ์ง‘ํ•ฉ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

a = {'name':'Kim', 'phone':'010-7777-7777', 'birth':890303}
b = {0:'Hello Python', 1:'Hello Coding'}
c = {'arr':[1,2,3,4,5]}
print(type(a), a) # <class 'dict'> {'name': 'Kim', 'phone': '010-7777-7777', 'birth': 890303}
print(a['name']) # Kim
print(a.get('name')) # Kim
print(a.get('address')) # None
print(c['arr'][1:2]) # [2]
a = set()
b = set([1,2,3,4])
c = set([1,4,5,6,6,4,5,6,1])
print(a) # set()
print(b) # {1, 2, 3, 4}
print(c) # {1, 4, 5, 6}
t = tuple(b)
l = list(c)
print(t) # (1, 2, 3, 4)
print(l) # [1, 4, 5, 6]
s1 = set([1,2,3,4,5,6])
s2 = set([4,5,6,7,8,9])
# ๊ต์ง‘ํ•ฉ
print(s1.intersection(s2)) # {4, 5, 6}
print(s1 & s2) # {4, 5, 6}
# ํ•ฉ์ง‘ํ•ฉ
print(s1.union(s2)) # {1, 2, 3, 4, 5, 6, 7, 8, 9}
print(s1 | s2) # {1, 2, 3, 4, 5, 6, 7, 8, 9}
# ์ฐจ์ง‘ํ•ฉ
print(s1.difference(s2)) # {1, 2, 3}
print(s1 - s2) # {1, 2, 3}


2. List ์™€ Tuple์˜ ์ฐจ์ด

๐Ÿค” ๋Œ€๊ด„ํ˜ธ๋Š” list, ์†Œ๊ด„ํ˜ธ๋Š” tuple

โœ”๏ธ list์™€ tuple์€ ์ˆœ์„œ๊ฐ€ ์žˆ๊ณ , ์ค‘๋ณต ๊ฐ€๋Šฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ณตํ†ต์ ์ด ์žˆ์œผ๋‚˜, list๋Š” ์ˆ˜์ •๊ณผ ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, tuple์€ ์ˆ˜์ • ๋ถˆ๊ฐ€, ์‚ญ์ œ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ ์ด์— list๋Š” ๊ฐ€๋ณ€ํ˜• ์ž๋ฃŒ ๊ตฌ์กฐ์ด๊ณ , tuple์€ ๋Œ€ํ‘œ์ ์ธ ๋ถˆ๋ณ€ํ˜• ์ž๋ฃŒ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

a = []
b = list()
c = [1, 2, 3, 4, 5]
d = [10, 100, 'Pen', 'Banana', 'Orange']
e = [10, 100, ['Pen', 'Banana', 'Orange']]
a = ()
b = (1,)
c = (1,2,3,4)
d = (10, 100, ('a', 'b', 'c'))
print(a,b,c) # () (1,) (1, 2, 3, 4)
print(d) # (10, 100, ('a', 'b', 'c'))
print(c[2]) # 3
print(c[3]) # 4
print(d[2][2]) # c
print(d[2:]) # (('a', 'b', 'c'),)
print(d[2][0:2]) # ('a', 'b')
print(c+d) # (1, 2, 3, 4, 10, 100, ('a', 'b', 'c'))

๐Ÿค” tuple์€ tuple ๋‚ด์˜ ์›์†Œ๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด, ๊ฐ™์€ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

โœ”๏ธ tuple์€ ๋ถˆ๋ณ€ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ์–ด์ฐจํ”ผ ๊ฐ’์˜ ์ˆ˜์ •์ด ๋ฐœ์ƒ๋˜๋ฉด ๋‹ค๋ฅธ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์ด ํŠน์ง•์„ ํ†ตํ•ด ํšจ์œจ์„ฑ์„ ์ตœ์ ํ™”์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์›์†Œ๊ฐ’๋“ค์ด ์ผ์น˜ํ•˜๋ฉด ๊ฐ™์€ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

t1 = (1, "์žฌ์›", "python")
t2 = (1, "์žฌ์›", "python")
print(t1, t2) # (1, '์žฌ์›', 'python') (1, '์žฌ์›', 'python')
print(t1 == t2) # True
print(id(t1), id(t2)) # 140294662918656 140294662918656
print(t1 is t2) # True

๐Ÿค” list๋Š” list ๋‚ด์˜ ์›์†Œ๊ฐ€ ๋™์ผํ•˜๋”๋ผ๋„, ๋…๋ฆฝ์ ์ธ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

โœ”๏ธ ์•„๋ž˜ 2๊ฐœ์˜ list๋Š” ๊ฐ™์€ ์›์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ๋น„๊ตํ•˜๋ฉด True๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€๋งŒ, ๊ฐ™์€ ๊ฐ์ฒด์ธ์ง€ id๊ฐ’์„ ํ™•์ธํ•˜๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

l1 = [1, "์žฌ์›", "python"]
l2 = [1, "์žฌ์›", "python"]
print(l1, l2) # [1, '์žฌ์›', 'python'] [1, '์žฌ์›', 'python']
print(l1 == l2) # True
print(id(l1), id(l2)) # 140368827609408 140368829161024
print(l1 is l2) # False

๐Ÿค” ๊ฐ์ฒด ์ฐธ์กฐ์— ์žˆ์–ด์„œ list, tuple์˜ ์ฐจ์ด

โœ”๏ธ ํ•จ์ˆ˜์˜ parameter๋กœ list์™€ tuple์ด ์ „๋‹ฌ๋  ๋•Œ ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

def plus(x, y):
    x += y
    return x

โœ”๏ธ tuple์€ ๋ถˆ๋ณ€ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ •์ด ๋ฐœ์ƒ๋˜๋ฉด ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์ƒˆ๋กœ์šด ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— id๊ฐ’์ด ๊ธฐ์กด๊ณผ ๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ์ฆ‰, ํ• ๋‹น์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ–ˆ์œผ๋‚˜ ํ• ๋‹น์˜ ๊ฐœ๋…์ด ์•„๋‹˜ ์žฌํƒ„์ƒ์œผ๋กœ ๋ณด๋Š”๊ฒŒ ๋งž์Šต๋‹ˆ๋‹ค.

t1 = (10, 100)
t2 = (5, 10)
print(plus(t1, t2), t1, t2)  # (10, 100, 5, 10) (10, 100) (5, 10)
print(id(plus(t1, t2)), id(t1), id(t2)) # 140515106090816 140515106054848 140515127551168

โœ”๏ธ list๋Š” ๊ฐ€๋ณ€ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— l1์˜ id๊ฐ’์— ํ• ๋‹น์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ˆ˜์ •์ด ์ด๋ค„์งˆ ๋•Œ, ๊ธฐ์กด id๊ฐ’์— ๋ฎ์–ด์”Œ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์›๋ณธ์˜ ๊ฐ’๋„ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.
โœ”๏ธ ์ด์— ์›๋ณธ์˜ ์ˆ˜์ •์„ ์›์น˜์•Š์œผ๋ฉด tuple, ์ˆ˜์ •๋˜๋„ ์ƒ๊ด€์—†์œผ๋ฉด list๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ

l1 = [10, 100]
l2 = [5, 10]
print(plus(l1, l2), l1, l2)  # [10, 100, 5, 10] [10, 100, 5, 10] [5, 10]
print(id(plus(l1, l2)), id(l1), id(l2))  # 140714272038400 140714272038400 140714272038592
profile
Keep Going, Keep Coding!

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