gpt4 book ai didi

python - 解包到列表中

转载 作者:太空狗 更新时间:2023-10-30 01:51:14 25 4
gpt4 key购买 nike

解包成元组在 Python 中有什么区别吗:

x, y, z = v

然后解包到一个列表中?

[x, y, z] = v

最佳答案

绝对没有,甚至是字节码(使用 dis ):

>>> def list_assign(args):
[x, y, z] = args
return x, y, z

>>> def tuple_assign(args):
x, y, z = args
return x, y, z

>>> import dis
>>> dis.dis(list_assign)
2 0 LOAD_FAST 0 (args)
3 UNPACK_SEQUENCE 3
6 STORE_FAST 1 (x)
9 STORE_FAST 2 (y)
12 STORE_FAST 3 (z)

3 15 LOAD_FAST 1 (x)
18 LOAD_FAST 2 (y)
21 LOAD_FAST 3 (z)
24 BUILD_TUPLE 3
27 RETURN_VALUE
>>> dis.dis(tuple_assign)
2 0 LOAD_FAST 0 (args)
3 UNPACK_SEQUENCE 3
6 STORE_FAST 1 (x)
9 STORE_FAST 2 (y)
12 STORE_FAST 3 (z)

3 15 LOAD_FAST 1 (x)
18 LOAD_FAST 2 (y)
21 LOAD_FAST 3 (z)
24 BUILD_TUPLE 3
27 RETURN_VALUE

关于python - 解包到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23831243/

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