目錄python
若是咱們給出一個列表,咱們須要一次性取出多個值,咱們是否是能夠用下面的方式實現呢?code
name_list = ['chen', 'python', 'jason'] x = name_list[0] y = name_list[1] z = name_list[2] print(f'x:{x}, y:{y}, z:{z}') #輸出: x:chen, y:python, z:jason
解壓縮能夠這樣理解:超市打包是把多個商品放在一塊兒,解壓縮其實就是解包把多個商品一次性拿出來。it
name_list = ['chen', 'python', 'jason'] x, y, z = name_list print(f'x:{x}, y:{y}, z:{z}') #輸出: x:chen, y:python, z:jason
有時候咱們解壓縮的值多是咱們不想要的,那麼能夠使用下劃線,萬能的下劃線。class
name_list = ['chen', 'python', 'jason'] x, y, z = name_list x, _, z = name_list # _至關於告訴計算機不要了,不能以_開頭和結尾 print(f'x:{x}, y:{y}, z:{z}') print(f'x:{x}, z:{z}') #輸出: x:chen, y:python, z:jason x:chen, z:jason
字典的解壓打包
dic = {"name":"chen","gender":"famale","age":18} x1,x2,x3 = dic print(x2) #輸出: gender
還能夠以下操做計算機
name_list = ['chen', 'python', 'jason','wang','lao'] x, y, _, _, z = name_list ## _是同樣的 x,* _, z = name_list print(f'x:{x}, y:{y}, z:{z}') print(f'x:{x}, z:{z}') #輸出: x:chen, y:python, z:lao x:chen, z:lao
python追求的是簡潔性,代碼不要寫的太長了。所以字典也是能夠的,可是字典解壓縮的是key。di
info = {'name': 'chen', 'age': 18} x, y = info print(x, y) #輸出: name age