1. 程式人生 > >python_cookbook之路:數據結構-解壓可叠代對象賦值給多個變量以及擴展的叠代解壓語法(*)

python_cookbook之路:數據結構-解壓可叠代對象賦值給多個變量以及擴展的叠代解壓語法(*)

rec 3.4 open pan spl python res cor coo

1.一一對應:

>>> data = [ ACME, 50, 91.1, (2012, 12, 21) ]
>>> name, shares, price, date = data
>>> name
ACME
>>> date
(2012, 12, 21)
>>> name, shares, price, (year, mon, day) = data
>>> name
ACME
>>> year
2012
>>> mon
12
>>> day
21 >>>
>>> data = [ ACME, 50, 91.1, (2012, 12, 21) ]
>>> name, shares, price, date = data
>>> name
ACME
>>> date
(2012, 12, 21)
>>> name, shares, price, (year, mon, day) = data
>>> name
ACME
>>> year
2012
>>> mon
12
>>> day
21 >>>

2.可叠代對象元素個數超過變量個數:,*匹配返回的是個列表

>>> record = (ACME, 50, 123.45, (12, 18, 2012))
>>> name, *_, (*_, year) = record
>>> name
ACME
>>> year
2012
>>>

python_cookbook之路:數據結構-解壓可叠代對象賦值給多個變量以及擴展的叠代解壓語法(*)