AI 빅데이터/Python 테크틱과 팁
[Zip] Starred Expression : Unzip하기
마고커
2024. 3. 7. 09:59
두개의 List를 묶어 Dictionary 형태로 만드려면 zip 함수를 사용하면 된다.
word = ['Pierre', 'Vinken', ',', '61', 'years', 'old', ',', 'will', 'join',
'the', 'board', 'as', 'a', 'nonexecutive', 'director', 'Nov.', '29', '.']
tag_info = ['NNP', 'NNP', ',', 'CD', 'NNS', 'JJ', ',', 'MD', 'VB',
'DT', 'NN', 'IN', 'DT', 'JJ', 'NN', 'NNP', 'CD', '.']
tagged = dict(zip(word, tag_info))
print(tagged.items())
==>
dict_items([('Pierre', 'NNP'), ('Vinken', 'NNP'), (',', ','), ('61', 'CD'),
('years', 'NNS'), ('old', 'JJ'), ('will', 'MD'), ('join', 'VB'),
('the', 'DT'), ('board', 'NN'), ('as', 'IN'), ('a', 'DT'),
('nonexecutive', 'JJ'), ('director', 'NN'), ('Nov.', 'NNP'),
('29', 'CD'), ('.', '.')])
그렇다면 묶여 있는 zip 형태를 풀어서 다시 list를 만들려면 어떻게 해야할까? zip 함수에 '*' 연산자를 사용하면 된다.
w, t = zip(*tagged.items())
print("word: ", w)
print("tag_info: ", t)
==>
word: ('Pierre', 'Vinken', ',', '61', 'years', 'old', 'will', 'join',
'the', 'board', 'as', 'a', 'nonexecutive', 'director', 'Nov.', '29', '.')
tag_info: ('NNP', 'NNP', ',', 'CD', 'NNS', 'JJ', 'MD', 'VB',
'DT', 'NN', 'IN', 'DT', 'JJ', 'NN', 'NNP', 'CD', '.')