为什么 x,y = zip(*zip(a,b)) 在 Python 中有效? [英] Why does x,y = zip(*zip(a,b)) work in Python?

查看:35
本文介绍了为什么 x,y = zip(*zip(a,b)) 在 Python 中有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我喜欢 Python 的 zip() 函数.一直在用,太棒了.我时不时地想做与 zip() 相反的事情,想想我曾经知道怎么做",然后 google python 解压缩,然后记住有人使用了这个神奇的 * 解压缩元组的压缩列表.像这样:

OK I love Python's zip() function. Use it all the time, it's brilliant. Every now and again I want to do the opposite of zip(), think "I used to know how to do that", then google python unzip, then remember that one uses this magical * to unzip a zipped list of tuples. Like this:

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)

这到底是怎么回事?那个神奇的星号在做什么?它还能应用在哪些地方?Python 中还有哪些其他令人惊叹的神奇事物如此神秘且难以用 google 搜索?

What on earth is going on? What is that magical asterisk doing? Where else can it be applied and what other amazing awesome things in Python are so mysterious and hard to google?

推荐答案

Python 中的星号记录在 Python 教程中,位于 解压参数列表.

The asterisk in Python is documented in the Python tutorial, under Unpacking Argument Lists.

这篇关于为什么 x,y = zip(*zip(a,b)) 在 Python 中有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆