python编程简单问题_很简单的python编程问题

当列表长度不同时,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]

>>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值