如何在 Python 中使用捆绑/打包生成器?
为了分摊函数调用开销,我更改了生成器,以便它生成一个包含几个值的固定长度列表,而不是一次生成一个值。生成器按照最初的样子,从包含多个 pickle 对象的文件中 unpickle 一个对象,并生成它。然后,这些内容在消耗生成器的 for 循环内进行处理。事实证明,这种方法比将对象处理代码放在手动展开的循环中(一次解封文件中的多个连续项目)要慢得多。我正在尝试妥协。我修改过的生成器一次生成一个固定长度的腌制对象列表。我正在寻找一种 Pythonic 方式来在消费者端解压未腌制的对象包。
有没有办法在没有额外嵌套循环的情况下解构这些数据包?我错误地认为 *
运算符会这样做:
for x in *packetizing_generator(): f(x)
嵌套循环当然可以工作,但我想知道是否有更短、更优雅的方法。
To amortize the function call overhead I have changed my generator so that it yields a fixed-length list of a few values instead of yielding one value at a time. The generator, as it originally stood, unpickled an object from a file that contains several pickled objects, and yielded it. These were then processed inside a for loop that consumed the generator. This approach turned out to be a lot slower than having the object processing code inside a hand unrolled loop that unpickled several consecutive items in the file at a time. I am attempting a compromise. My modified generator yields a fixed-length list of pickled objects at a time. I am looking for a Pythonic way to unpack that packet of unpickled objects at the consumer side.
Is there a way to deconstruct those packets without having an extra nested loop ? I incorrectly assumed that the *
operator will do it like so:
for x in *packetizing_generator(): f(x)
Nested loop of course works, but am wondering if there is a shorter and more elegant way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想这就是你所说的,如果我不正确,请告诉我。
假设
your_generator
生成的结果恰好是 3 个项目的序列。您可以使用以下方法构建一个一次恰好生成
n
个项目的生成器,以便在for
循环中使用:如果您的生成器在某个时间没有生成单个项目,时间,并且您想要更改它生成的项目数,请使用以下命令:
这是一个示例:
这需要一个最初一次生成两个项目的生成器,而是一次生成四个元素。
I think this is what you are talking about, let me know if I'm not on the right track.
Assumes
your_generator
yields results as a sequence of exactly 3 items.You can construct a generator that yields exactly
n
items at a time to use in yourfor
loop with the following method:And if your generator does not yield a single item at a time, and you want to change the number of items it yields you use the following:
Here is an example:
This takes a generator that originally yielded items two at a time, and instead yields elements four at a time.