如何从Python的特定点开始Itertools循环?
我已经使用以下代码为英语字母创建了一个itertools
循环,
lowercase_letters_cycle = itertools.cycle(string.ascii_lowercase)
如果我在此迭代对象上运行循环 loop,则第一个迭代会给我“ a”输出是因为周期从“ A”开始。我该如何做到这一点,以便循环从我选择的任何字母开始?
有效的一种方法是,
def start_cycle(letter):
lowercase_letters_cycle = itertools.cycle(lowercase_letters)
letter_index = lowercase_letters.index(letter)
index = 0
while True:
if index == letter_index:
break
letter = next(lowercase_letters_cycle)
index += 1
return lowercase_letters_cycle
但是有任何较短的方法吗?
I have created an itertools
cycle for the English alphabet using the code below,
lowercase_letters_cycle = itertools.cycle(string.ascii_lowercase)
If I run a for
loop on this iterator object, the first iteration would give me "a" as the output because the cycle starts from "a". How can I make it so that cycle starts from any letter of my choice?
One way that works is,
def start_cycle(letter):
lowercase_letters_cycle = itertools.cycle(lowercase_letters)
letter_index = lowercase_letters.index(letter)
index = 0
while True:
if index == letter_index:
break
letter = next(lowercase_letters_cycle)
index += 1
return lowercase_letters_cycle
But is there any shorter method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
itertools
文档提供A 用于从迭代器中食用许多项目。因此,您可以创建周期,然后在继续之前消耗一些周期。
相同的
消费
也可以从第三方更多 - itertools
软件包。The
itertools
documentation supplies a recipe for consuming a number of items from an iterator.So you create the cycle, then just consume a bit of it before proceeding.
The same
consume
is also available from the third-partymore-itertools
package.您可以从
itertools
模块中组合ISLICE
cyce cyce ,如下所示:它将产生
d
(来源的字符在第一个3)之后,然后e
,....,然后z
,然后a
,然后b
, 等等。您可以将第二个参数中的数字更改为islice
以从不同的字母开始。You can combine
islice
andcycle
from theitertools
module as follows:It will yield
d
(the character that comes after the first 3), thene
, ...., thenz
, thena
, thenb
, and so on. You can change the number in the second argument toislice
to start from a different letter.您可以将输入列表切成小块,以从您选择的索引开始。
然后,
打印:
You could just slice the input list to start at the index of your choice.
Then,
prints:
itertools.drophiles.dropwhiles.dropwhile
其前哨功能的效果结果 - 在这种情况下,lambda x:x!= letter
,并将“吞咽”结果,直到函数首次返回false
。那时该功能不再被调用,并且可以进行,从而产生任何
进一步的值。
另外,请注意,无需将
ascii_lowercase
转换为列表,因为字符串已经是迭代的。
请记住,如果通过ASCII_LOWERCASE中的字符,这将陷入100%CPU的无限循环中。最好用一张检查:
itertools.dropwhile
will feed the iterable results to its sentinel function - in this caselambda x: x != letter
, and will "swallow" the results until the function returnsFalse
for the first time. At that pointthe function is no longer called, and the iterable proceeds, yielding any
further values.
Also, note there is no need to convert
ascii_lowercase
to a list,as strings are already iterables.
Keep in mind that this will run into an infinite loop with 100% CPU if a character not in ascii_lowercase is passed. It is better to guard it with a check:
如果您只想从特定点开始浏览列表,则可以这样做:
If you just want to run through a list once starting at a particular point you can do: