当列表长度不同时,zip能自动按短列表长度处理数据。
若“前提是两组list的长度一样”,请描述长度不同时的处理方式 返回什么消息或者提出什么异常?$ python
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def average_daily_temp(high_temps, low_temps):
... """ (list of number, list of number) -> list of float
... Precondition: len(high_temps) == len(low_temps)
... high_temps and low_temps are daily high and low temperatures for a series
... of days. Return a new list of temperatures where each item is the daily
... average.
...
... >>> average_daily_temp([26, 27, 27, 28, 27, 26], [20, 20, 20, 20, 21, 21])
... [23.0, 23.5, 23.5, 24.0, 24.0, 23.5]
... """
... def _avg((high_temp, low_temp)):
... return (high_temp + low_temp) / 2.
... return map(_avg, zip(high_temps, low_temps))
...
>>> average_daily_temp([26, 27, 27, 28, 27, 26], [20, 20, 20, 20, 21, 21])
[23.0, 23.5, 23.5, 24.0, 24.0, 23.5]
>>>