Python列表中的命名元素

Ale*_*ham 0 python

在PHP中,我可以在列表中命名元素,如:

<?php
    list($name,$address,$home) = array("Alex","Taiwan","Vietnam");
    echo $address."\n";
?>
Run Code Online (Sandbox Code Playgroud)

产量:台湾

它可以在Python中使用相同的功能吗?我厌倦了这样做:

list = ["Alex","Taiwan","Vietnam"]
print list[1] 
Run Code Online (Sandbox Code Playgroud)

let*_*utx 6

你可以使用拆包.

name, city, country = ["Alex","Taiwan","Vietnam"]
print name
Run Code Online (Sandbox Code Playgroud)

在Python3中,您可以使用*运算符执行更多操作.

a, *b, c = [1, 2, 3, 4, 5] # a = 1, b = [2, 3, 4], c = 5
Run Code Online (Sandbox Code Playgroud)

有关更多概括,请参阅PEP 448 - 其他解包概括.