为了账号安全,请及时绑定邮箱和手机立即绑定

itertools 库中的 tee() 函数

itertools 库中的 tee() 函数

aluckdog 2022-11-24 15:03:04

这是一个从列表中获取最小值、最大值和平均值的简单示例。下面的两个函数有相同的结果。我想知道这两个函数之间的区别。为什么要使用itertools.tee()?它提供了什么优势?


from statistics import median

from itertools import tee


purchases = [1, 2, 3, 4, 5]


def process_purchases(purchases):

    min_, max_, avg = tee(purchases, 3)

    return min(min_), max(max_), median(avg)


def _process_purchases(purchases):

    return min(purchases), max(purchases), median(purchases)


def main():

    stats = process_purchases(purchases=purchases)

    print("Result:", stats)

    stats = _process_purchases(purchases=purchases)

    print("Result:", stats)


if __name__ == '__main__':

    main()


查看完整描述

1 回答

?
MYYA

TA贡献1588条经验 获得超4个赞

迭代器在 python 中只能迭代一次。之后,它们“耗尽”并且不再返回更多值。


map()您可以在、和许多其他函数zip()中看到这一点:filter()


purchases = [1, 2, 3, 4, 5]


double = map(lambda n: n*2, purchases)


print(list(double))

# [2, 4, 6, 8, 10]


print(list(double))

# [] <-- can't use it twice

如果将迭代器传递给两个函数,例如 的返回值,您可以看到它们之间的区别map()。在这种情况下_process_purchases()失败,因为用尽了迭代器并且没有为和min()留下任何值。max()median()


tee()接受一个迭代器并给你两个或更多,允许你多次使用传递给函数的迭代器:


from itertools import tee

from statistics import median


purchases = [1, 2, 3, 4, 5]


def process_purchases(purchases):

    min_, max_, avg = tee(purchases, 3)

    return min(min_), max(max_), median(avg)



def _process_purchases(purchases):

    return min(purchases), max(purchases), median(purchases)


double = map(lambda n: n*2, purchases)

_process_purchases(double)

# ValueError: max() arg is an empty sequence


double = map(lambda n: n*2, purchases)

process_purchases(double)

# (2, 10, 6)


查看完整回答
反对 回复 2022-11-24

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信