itertools“上一个"(与下一个相反)python [英] itertools 'previous' (opposite of next) python

查看:69
本文介绍了itertools“上一个"(与下一个相反)python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用

>> import itertools
>> ABC = [a, b, c]
>> abc = itertools.cycle( ABC )
>> next( abc )
a
>> next( abc )
b    
>> next( abc )
c

我希望下一个电话

>> previous( abc )
b

itertools中是否有一种方法可以做到这一点?

Is there a method in itertools that can accomplish this?

推荐答案

不,没有.

由于Python的迭代协议的工作方式,如果不保留所生成值的整个历史记录,就不可能实现previous. Python不会这样做,并且鉴于内存需求,您可能不希望这样做.

Because of the way Python's iteration protocol works, it would be impossible to implement previous without keeping the entire history of the generated values. Python doesn't do this, and given the memory requirements you probably wouldn't want it to.

这篇关于itertools“上一个"(与下一个相反)python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆