intoducing python
Dictionaries
#튜플 생성
empty_tuple = ()
one_tuple = ('test')
#multiple variables assign
test_tuple = ('apple', 'banana', 'mango')
a, b, c = test_tuple
print(a)
>>> apple
print(b)
>>> banana
#리스트로 튜플 생성
test_list = ['apple', 'banana', 'mango']
tuple(test_tuple)
print(test_tuple)
>>> ('apple', 'banana', 'mango')
#Dict 생성
empty_dict = {}
#리스트를 Dict로 convert
1)
test_list = [[1,2],[3,4],[5,6]]
dict(test_list)
>>> {1:2, 3:4, 5:6}
2)
test_list = [(1, 2),(3, 4),(5, 6)]
dict(test_list)
>>> {1:2, 3:4, 5:6}
3)
test_list = ([1,2], [3,4], [5,6])
dict(test_list)
>>> {1:2, 3:4, 5:6}
4)
test_list = ['ab', 'cd', 'ef']
dict(test_list)
>>> {'a':'b', 'c':'d', 'e':'f'}
5)
test_list = ('ab', 'cd', 'ef')
dict(test_list)
>>> {'a':'b', 'c':'d', 'e':'f'}
#Dict 2개 합치기
a_dict={'apple':1, 'banana':2}
b_dict={'mango':3}
a_dict.update(b_dict)
print(a_dict)
>>> {'apple':1, 'banana':2, 'mango':3}
#Dict Key 삭제
test_dict = {'apple':1, 'banana':2, 'mango':3}
del test_dict['apple']
print(test_dict)
>>> {'banana':2, 'mango':3}
#Dict 초기화
test_dict = {'apple':1, 'banana':2, 'mango':3}
test_dict.clear()
print(test_dict)
>>> {}
#Dict Key 만 받아오기
test_dict = {'apple':1, 'banana':2, 'mango':3}
print(test_dict.keys())
>>> dict_keys(['apple', 'banana', 'mango'])
#Dict Value만 받아오기
test_dict = {'apple':1, 'banana':2, 'mango':3}
print(list(test_dict.values())
>>> [1, 2, 3]
#Dict Key,Value 받아오기
test_dict = {'apple':1, 'banana':2, 'mango':3}
print(list(test_dict.items())
>> [('apple', 1), ('banana', 2), ('mango', 3)]