[Python基础] 可变长度序列赋值

>>> def drop_frist_last(nums):
...     first, *middle, last = nums
...     return sum(middle)/len(middle)

>>> drop_frist_last([1,2,3])
2.0
>>> drop_frist_last([1,2,3,4])
2.5

>>> record = ('Hello', 'a@qq.com', 123,456)
>>> name, email, *phone_numbers = record
>>> name, email, phone_numbers
('Hello', 'a@qq.com', [123, 456])
>>> record = ('Hello', 'a@qq.com', 123)
>>> name, email, *phone_numbers = record
>>> name, email, phone_numbers
('Hello', 'a@qq.com', [123])
>>> record = ('Hello', 'a@qq.com')
>>> name, email, *phone_numbers = record
>>> name, email, phone_numbers
('Hello', 'a@qq.com', [])
>>>
 

上一篇:python之网络爬虫


下一篇:P5 【Python爬虫】HTML与Beautiful Soup4 解析