Skip to main content
 首页 » 编程设计

python之将列表赋值给变量后跟一个逗号相当于访问列表的入口,这种Python特性叫什么名字

2024年11月01日10lovecherry

这是一个简单的列表

>>> list_1 = [1] 
>>> list_1 
[1] 

将列表分配给后跟逗号的变量

>>> tmp, = list_1 
>>> tmp 
1 

相当于访问列表的入口

这种特征的名称是什么。

请您参考如下方法:

在Python中叫做unpacking。

def fn(one, two, three): 
    print(one, two, three) 
 
my_args = [1, 2, 3] 
 
fn(*my_args) 
 
one, two, three = my_args 
print(one, two, three) 
 
fn(one, two, three) 

输出:

1 2 3 
1 2 3 
1 2 3 

更多详情:

  • 阅读PEP-3132 ,正如克里斯在评论中提到的那样。
  • 在互联网上搜索 Python 将列表解压缩为变量。