如何查看迭代器的索引?

Sim*_*ver 0 python iterator

所以我有一个字符串source,我用迭代器循环:

iterator = iter(source)
for char in iterator:
  do stuff
Run Code Online (Sandbox Code Playgroud)

但是,现在说我有一个签入do stuff,我将迭代器的值与'h'进行比较.然后我想以某种方式看看'h'是否后跟"ello",然后将前十个字符添加到列表中.
为此,我自己的想法是找出哪个索引与迭代器的当前位置相对应,以便我可以说:

indIt = index(char)
if source[indIt + 1: indIt + 6] == "ello ":
  someList.append(source[indIt + 7:indIt + 16])
  indIt += 17
  char = indIt #which may also be fun to know how it can be done, if
Run Code Online (Sandbox Code Playgroud)

这意味着对于给定的输入hello Sandra, oh and hello Oscar, i welcome you both!,someList将包含["Sandra,oh","Oscar,i w"].

那么,我可以通过某种方式确定迭代器当前位置对应的索引吗?

Mar*_*ers 6

迭代器不公开的指数,因为那里并没有成为一个序列根本之一.

使用该enumerate()函数在迭代中添加一个:

for index, char in enumerate(iterator):
Run Code Online (Sandbox Code Playgroud)

现在迭代生成(index, value)元组,您可以使用元组赋值将其分配给两个单独的变量.