如何在Python中修改生成器?

编程入门 行业动态 更新时间:2024-10-26 18:15:43
本文介绍了如何在Python中修改生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Python中是否可以使用一个通用接口来修改生成器的行为?

Is there a common interface in Python that I could derive from to modify behavior of a generator?

例如,我想修改一个现有的生成器,以便在流中插入一些值,并删除一些其他值.

For example, I want to modify an existing generator to insert some values in the stream and remove some other values.

我该怎么做?

感谢Boda Cydo

Thanks, Boda Cydo

推荐答案

您可以使用 itertools 生成一个发电机并产生一个新的发电机.

You can use the functions provided by itertools to take a generator and produce a new generator.

例如,您可以使用 takewhile 直到谓词为no不再满足,并且链包含一系列新值.

For example, you can use takewhile until a predicate is no longer fulfilled, and chain on a new series of values.

看看其他示例的文档,包括 ifilter , dropwhile 和 islice 仅举几例.

Take a look at the documentation for other examples, including things like ifilter, dropwhile and islice to name just a few more.

更多推荐

如何在Python中修改生成器?

本文发布于:2023-11-10 09:22:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生成器   如何在   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!