๐zip(*iterables)
iterable ์๋ฃํ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋ฐ์, ๋์ผํ ์ธ๋ฑ์ค ์์น์ ์๋ ๊ฐ๊ฐ์ element๋ฅผ ํํ๋ก ๋ฌถ์ด ์๋ก์ด iterable ์๋ฃํ์ ์์ฑํ๋ ํจ์
fruit = ["apple", "orange", "banana"]
color = ["red", "orange", "yellow"]
sweety = [30, 10, 1]
zipper = zip(fruit, color, sweety)
# [('apple', 'red', 30), ('orange', 'orange', 10), ('banana', 'yellow', 1)]- return zip ๊ฐ์ฒด
iterable ์๋ฃํ์ ๊ฐ์๊ฐ ๋์ผํ์ง ์๋ค๋ฉด?
๊ฐ์ฅ ์งง์ iterable ๊ฐ์ฒด๊ฐ ์๋ฃ๋ ๋๊น์ง๋ง ์คํ ๊ธด ์๋ฃํ๋ค์ ๋๋จธ์ง element๋ค์ ์ฌ์ฉ๋์ง ์์
๐์ฌ์ฉ ์์
๐Dictionary ๋ง๋ค๊ธฐ
fruit = ["apple", "orange", "banana"]
color = ["red", "orange", "yellow"]
dict_zip = {f: c for f, c in zip(fruit, color)}
# {'apple': 'red', 'orange': 'orange', 'banana': 'yellow'}๐with enumerate
fruit = ["apple", "orange", "banana"]
color = ["red", "orange", "yellow"]
sweety = [30, 10, 1]
for i, (f, s) in enumerate(zip(fruit, sweety)):
print(i, f, s)
# 0 apple 30
# 1 orange 10
# 2 banana 1- for๋ฌธ ์์ zip์ผ๋ก ๋ฌถ์ด์ค ๋ถ๋ถ์ ๋ณ์๋ ์๊ดํธ
()๋ก ๋ฌถ์ด์ค์ผ ํจ ๋ฌถ์ง ์๋๋ค๋ฉด ํํํ์์ผ๋ก ์ฌ์ฉz[1],z[2]