[Python] Dictionary 1

yuuforest·2023년 6월 29일
0

Python 문법

목록 보기
11/17
post-thumbnail

dict, key 정렬, key 기준 정렬, value 기준 정렬

🍦 dict


✔️ 빈 Dict

N = dict()

>> {}
N = {}

>> {}

✔️ 원소 선언
key와 value로 숫자, 문자, 튜플 등 가능

N = {'a': 1, "b" : (2, 3), 4 : 'd', (5, 6) : 'e'}

>> {'a': 1, 'b': (2, 3), 4: 'd', (5, 6): 'e'}
N = dict([('a', 1), ("b", (2, 3)), (4, 'd'), ((5, 6), 'e')])

>> {'a': 1, 'b': (2, 3), 4: 'd', (5, 6): 'e'}
N = dict(a=1, b=2, c=3, d=4)		# key가 문자열인 경우만 가능

>> {'a': 1, 'b': 2, 'c': 3, 'd': 4}

✔️ List

N = dict([(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')])	# 튜플로 구성된 경우에만 가능

>> {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
N = dict([1, 2, 3, 4])	# 튜플로 구성되지 않은 경우 변환 불가능

>> TypeError: cannot convert dictionary update sequence element #0 to a sequence

✔️ Set

N = dict({(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')})	# 튜플로 구성된 경우에만 가능

>> {2: 'b', 1: 'a', 4: 'd', 3: 'c'}
N = dict({1, 2, 3, 4})	# 튜플로 구성되지 않은 경우 변환 불가능

>> TypeError: cannot convert dictionary update sequence element #0 to a sequence

✔️ zip

key = [1, 'a', "b", (2, 3)]
value = [("c", "d"), 4, "5", 10]

N = dict(zip(key, value))

>> {1: ('c', 'd'), 'a': 4, 'b': '5', (2, 3): 10}
key = [1, 'a', "b", (2, 3)]
value = [("c", "d"), 4, "5"]

N = dict(zip(key, value))	# 개수가 동일하지 않을 경우 더 적은 개수로 맞춰서 만들어짐

>> {1: ('c', 'd'), 'a': 4, 'b': '5'}

🍦 Key 정렬


✔️ 정수

d = {3 : 'c', 1 : 'a', 4 : 'd',  2 : 'b'}

N = sorted(d)					# 오름차순(ASC)			

>> [1, 2, 3, 4]

N = sorted(d, reverse=True)		# 내림차순(DES)

>> [4, 3, 2, 1]

✔️ 문자열

d = {'c' : 3, 'a' : 1, 'd' : 4,  'b' : 2}

N = sorted(d)					# 오름차순(ASC)			

>> ['a', 'b', 'c', 'd']

N = sorted(d, reverse=True)		# 내림차순(DES)	

>> ['d', 'c', 'b', 'a']

🍦 Key, Value 정렬 (Key 기준)


✔️ 정수

d = {3 : 's', 1 : 'z', 4 : 'w',  2 : 'y'}

N = sorted(d.items())									# 오름차순(ASC)

>> [(1, 'z'), (2, 'y'), (3, 's'), (4, 'w')]

N = sorted(d.items(), reverse=True)						# 내림차순(DES)

>> [(4, 'w'), (3, 's'), (2, 'y'), (1, 'z')]
d = {3 : 's', 1 : 'z', 4 : 'w',  2 : 'y'}

N = sorted(d.items(), key=lambda x:x[0])				# 오름차순(ASC)

>> [(1, 'z'), (2, 'y'), (3, 's'), (4, 'w')]

N = sorted(d.items(), key=lambda x:x[0], reverse=True)	# 내림차순(DES)

>> [(4, 'w'), (3, 's'), (2, 'y'), (1, 'z')]

✔️ 문자열

d = {'c' : 23, 'a' : 34, 'd' : 45,  'b' : 56}

N = sorted(d.items())									# 오름차순(ASC)

>> [('a', 34), ('b', 56), ('c', 23), ('d', 45)]

N = sorted(d.items(), reverse=True)						# 내림차순(DES)

>> [('d', 45), ('c', 23), ('b', 56), ('a', 34)]
d = {'c' : 23, 'a' : 34, 'd' : 45,  'b' : 56}

N = sorted(d.items(), key=lambda x:x[0])				# 오름차순(ASC)

>> [('a', 34), ('b', 56), ('c', 23), ('d', 45)]

N = sorted(d.items(), key=lambda x:x[0], reverse=True)	# 내림차순(DES)

>> [('d', 45), ('c', 23), ('b', 56), ('a', 34)]

🍦 Key, Value 정렬 (Value 기준)


✔️ 정수

d = {'c' : 3, 'a' : 2, 'd' : 1,  'b' : 4}

N = sorted(d.items(), key=lambda x:x[1])				# 오름차순(ASC)

>> [('d', 1), ('a', 2), ('c', 3), ('b', 4)]

N = sorted(d.items(), key=lambda x:x[1], reverse=True)	# 내림차순(DES)

>> [('b', 4), ('c', 3), ('a', 2), ('d', 1)]

✔️ 문자열

d = {3 : 'c', 1 : 'b', 4 : 'a',  2 : 'd'}

N = sorted(d.items(), key=lambda x:x[1])				# 오름차순(ASC)

>> [(4, 'a'), (1, 'b'), (3, 'c'), (2, 'd')]

N = sorted(d.items(), key=lambda x:x[1], reverse=True)	# 내림차순(DES)

>> [(2, 'd'), (3, 'c'), (1, 'b'), (4, 'a')]
profile
🐥 Backend Developer 🐥

0개의 댓글