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 >>>
原文地址:https://www.cnblogs.com/wuchenggong/p/9190873.html
时间: 2024-11-11 18:28:44