๋ฆฌ์คํธ 2๊ฐ๋ฅผ ๋์ ๋๋ฆฌ๋ก ๋ณํ
>>> pro = ['a', 'b', 'c']
>>> sp = [1, 2, 3]
>>>
>>> dic = { pro[i] : sp[i] for i in range(len(pro))}
>>>
>>> dic
{'a': 1, 'b': 30, 'c': 5}
๋์ ๋๋ฆฌ์ ๋ฆฌ์คํธ ์ถ๊ฐํ๊ธฐ
๋ค์ ๋ฐฉ๋ฒ์ ์ฌ๋ฌ๊ฐ์ง ๊ธฐ์ค์ผ๋ก ๋ถ๋ฅ ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
>>> dic = {}
>>> dic['fruit'] = ['apple']
>>> dic['fruit'].append('banana')
>>> dic
{'fruit': ['apple', 'banana']}
๋ค์๊ณผ ๊ฐ์ด put ๋ฆฌ์คํธ๊ฐ ์ฃผ์ด์ง ๋
put = [['fruit','apple'],['fruit','bananan'],['fruit','melon'],['animal','rabbit'],['animal','tiger']]
key๊ฐ "fruit"์ธ ์์์ apple, banana, melon ์ ๋ฃ๊ณ ์ถ๊ณ ,
key๊ฐ "animal"์ธ ์์์ rabbit, tiger ์ ๋ฃ๊ณ ์ถ์ ๋ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
>>> dic = {}
>>> for mx in put:
if mx[0] in dic.keys():
dic[mx[0]].append(mx[1])
else:
dic[mx[0]] = [mx[1]]
>>> dic
{'fruit': ['apple', 'bananan', 'melon'], 'animal': ['rabbit', 'tiger']}
'Developer > ๐ฆ python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ด์ฌ(python) - ๊ธฐ๋ณธ ๋ฌธ๋ฒ ์ ๋ฆฌ (3) (0) | 2020.06.21 |
---|---|
ํ์ด์ฌ(python) - ๊ธฐ๋ณธ ๋ฌธ๋ฒ ์ ๋ฆฌ (2) (1) | 2020.06.18 |
ํ์ด์ฌ(python) - ๋์ ๋๋ฆฌ(dictionary) ๋ฌธ๋ฒ ๊ธฐ๋ณธ (0) | 2020.06.14 |
ํ์ด์ฌ(python) - IDLE ๋ค์ฌ์ฐ๊ธฐ ๋จ์ถํค (0) | 2020.06.14 |
ํ์ด์ฌ(python) - ๊ธฐ๋ณธ ๋ฌธ๋ฒ ์ ๋ฆฌ (1) (0) | 2020.06.14 |