gpt4 book ai didi

python - 为什么 x,y = zip(*zip(a,b)) 在 Python 中工作?

转载 作者:IT老高 更新时间:2023-10-28 21:33:16 28 4
gpt4 key购买 nike

好的,我喜欢 Python 的 zip() 函数。一直用,很厉害。时不时我想做与zip()相反的事情,想“我以前知道怎么做”,然后google python unzip,然后记住一个使用这个神奇的* 解压缩元组的压缩列表。像这样:

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 中还有哪些令人惊奇的、令人敬畏的东西如此神秘且难以用谷歌搜索?

最佳答案

Python 中的星号记录在 Python 教程中,位于 Unpacking Argument Lists 下.

关于python - 为什么 x,y = zip(*zip(a,b)) 在 Python 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2511300/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com