리스트를 딕셔너리로 변환
리스트 하나를 딕셔너리로
string_list = ['A','B','C']
string_list = ['A','B','C']
dictionary = {string : 0 for string in string_list}
print(dictionary)
{'A': 0, 'B': 0, 'C': 0}
string_list = ['A','B','C']
dictionary = {string : i for i,string in enumerate(string_list)}
print(dictionary)
{'A': 0, 'B': 1, 'C': 2}
string_list = ['A','B','C']
dictionary = dict.fromkeys(string_list,0)
print(dictionary)
{'A': 0, 'B': 0, 'C': 0}
string_list = ['A','B','C']
dictionary = dict.fromkeys(string_list)
print(dictionary)
{'A': None, 'B': None, 'C': None}
리스트 두개를 딕셔너리로
string_list = ['A','B','C']
int_list = [1, 2, 3]
- zip
string_list = ['A','B','C']
int_list = [1, 2, 3]
dictionary = dict(zip(string_list, int_list))
print(dictionary)
{'A': 1, 'B': 2, 'C': 3}
string_list = ['A','B','C']
int_list = [1, 2, 3]
dictionary = dict(zip(int_list, string_list))
print(dictionary)
{1: 'A', 2: 'B', 3: 'C'}