다른 명령
zip() 함수
개요
In Python 3 zip returns an iterator instead and needs to be passed to a list function to get the zipped tuples:
x = [1, 2, 3]; y = ['a','b','c'] z = zip(x, y) z = list(z) print(z) >>> [(1, 'a'), (2, 'b'), (3, 'c')]
Then to unzip them back just conjugate the zipped iterator:
x_back, y_back = zip(*z) print(x_back); print(y_back) >>> (1, 2, 3) >>> ('a', 'b', 'c')
If the original form of list is needed instead of tuples:
x_back, y_back = zip(*z) print(list(x_back)); print(list(y_back)) >>> [1,2,3] >>> ['a','b','c']