相关疑难解决方法(0)

在'for'循环中访问索引?

如何访问索引本身以获取如下列表?

ints = [8, 23, 45, 12, 78]
for i in ints:
    print('item #{} = {}'.format(???, i))
Run Code Online (Sandbox Code Playgroud)

当我使用循环遍历它时for,如何访问循环索引,在这种情况下从1到5?

python loops list

3312
推荐指数
22
解决办法
194万
查看次数

如何查看迭代器的索引?

所以我有一个字符串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"].

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

python iterator

0
推荐指数
1
解决办法
65
查看次数

标签 统计

python ×2

iterator ×1

list ×1

loops ×1