# 为什么x，y = zip（* zip（a，b））在Python中起作用？

` `x = [1,2,3] y = [4,5,6] zipped = zip(x,y) unzipped_x, unzipped_y = zip(*zipped) unzipped_x Out[30]: (1, 2, 3) unzipped_y Out[31]: (4, 5, 6)` `

### 6 Solutions collect form web for “为什么x，y = zip（* zip（a，b））在Python中起作用？”

Python中的星号logging在Python教程中的Unpacking Argument Lists下 。

` `def foo(*args): print args foo(1, 2, 3) # (1, 2, 3) # also legal t = (1, 2, 3) foo(*t) # (1, 2, 3)` `

` `def foo(**kwargs): print kwargs foo(a=1, b=2) # {'a': 1, 'b': 2} # also legal d = {"a": 1, "b": 2} foo(**d) # {'a': 1, 'b': 2}` `

` `def foo(*args, **kwargs): print args, kwargs foo(1, 2, a=3, b=4) # (1, 2) {'a': 3, 'b': 4}` `

` `>>> x = [] >>> y = [] >>> zipped = zip(x, y) >>> unzipped_x, unzipped_y = zip(*zipped) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 0 values to unpack` `

` `>>> unzipped_x, unzipped_y = zip(*zipped) or ([], []) >>> unzipped_x [] >>> unzipped_y []` `

` `>>> unzipped_x, unzipped_y = tuple(zip(*zipped)) or ([], [])` `

` `>>> x = [1, 2, 3] >>> y = "abc" >>> zipped = zip(x, y) >>> zipped [(1, 'a'), (2, 'b'), (3, 'c')] >>> z1, z2, z3 = zip(x, y) >>> z1 (1, 'a') >>> z2 (2, 'b') >>> z3 (3, 'c') >>> rezipped = zip(*zipped) >>> rezipped [(1, 2, 3), ('a', 'b', 'c')] >>> rezipped2 = zip(z1, z2, z3) >>> rezipped == rezipped2 True` `

@ bcherry的附录答案：

` `>>> def f(a2,a1): ... print a2, a1 ... >>> d = {'a1': 111, 'a2': 222} >>> f(**d) 222 111` `

• 我如何运行一个Python程序？
• 从Python中的“with”块（以及为什么）产生安全吗？
• 使用python向RESTful API发出请求
• urllib和“SSL：CERTIFICATE_VERIFY_FAILED”错误
• Python Pandas - 根据列名重新排列数据框中的列
• Linux SDK升级后的“ImportError：No module named webapp2”（1.9.35 - > 1.9.38）
• 如何在matplotlib中添加第二个x轴
• django模板中的“none”是什么？
• 有条件地replacepandas
• tic，toc在Python中起着模拟的作用
• 我如何处理Python中的列表理解exception？