Py学习  »  Python

跳过python中的索引

DC55 • 4 年前 • 868 次点击  

我创建的代码的最后一部分有问题。例如,我试图使列表正常地迭代到项目3,但是检查项目是否为3以及其他条件(现在不重要),然后将索引从示例10更改为迭代。

我做了很多尝试,但似乎不起作用。

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
for i in range(0,len(li)):
    print(i)
    if i == 3: #along with other condition
        def g(li):
            global i
            i = li[9]
        g()
        print(i)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38841
 
868 次点击  
文章 [ 8 ]  |  最新文章 4 年前
tobias_k
Reply   •   1 楼
tobias_k    5 年前

你想做的事情对于一条巨蟒来说是不可能的。 for 循环,它小于Java/C风格 for (initializer; step; condition) 循环,但更像是 foreach (iterable) 循环,其中iterable恰好是 range 就你而言。

因此,无论何时 i = ... 在你的循环中( i 是来自 对于 循环) 将在循环的下一次迭代中用新值覆盖(未修改)。

相反,您可以使用稍长一点的 while 循环:

i = 0
while i < len(li):
    print(i)
    if i == 3: #along with other condition
        def g(li):
            global i
            i = li[9]
        g(li)
    else:
        i += 1

还要注意嵌套函数 g 尽管实际代码中的情况可能不同,但显然不起任何作用,可以将其删除。

i = 0
while i < len(li):
    print(i)
    if i == 3: #along with other condition
        i = li[9]
    else:
        i += 1
jpp
Reply   •   2 楼
jpp    5 年前

for 不需要循环。如果您喜欢使用第三方库,可以使用numpy:

import numpy as np

A = np.array(li)

res = A[np.r_[:4, 9:len(A)]]

# array([ 1,  2,  3, 10, 11, 12, 13, 14, 15, 16])

或者使用普通的python slice 物体:

from operator import itemgetter
from itertools import chain

slices = (slice(0, 4), slice(9, None))

res = list(chain.from_iterable(itemgetter(*slices)(li)))

# [1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16]
kcorlidy
Reply   •   3 楼
kcorlidy    5 年前

我试着理解你说的话,我写了这段代码

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
def k(pos = 0):
    for i in li[pos:]:
        print(i)
        if li.index(i) == 3: #along with other condition
            return k(pos = 9)
k()
Philip DiSarro
Reply   •   4 楼
Philip DiSarro    5 年前

最简单易读的方法是通过使用 while 而不是 for 循环如下所示。

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
i = 0
while i < len(li):
    print(i)
    if i == 3:
        i = 10
        print(i)
    i += 1 # increment i at the end of the loop
Nuts
Reply   •   5 楼
Nuts    5 年前

应使用continue,如下所示:

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
for i in range(0,len(li)):
    if i in range(3, li[9]):
        continue
    print(i)
Landar
Reply   •   6 楼
Landar    5 年前
li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
condition3_reached = False
condition10_reached = False
for i in li:
    print(i)
    if conditition3_reached and not condition10_reached and i != 10:
        continue
    if condition3_reached and i == 10:
        condition10_reached = True
    if i == 3 and (#along with other condition):
        condition3_reached = True
        print(i)
    else:
        do_some_new_thing_for_10_onwards()

这是一个简单的实现你想要的东西的方法。我担心它不可扩展

c.w
Reply   •   7 楼
c.w    5 年前

您正在将i的值设置为for循环中的某个值。在for循环的每次迭代开始时,python都会更新i的值,使其成为要迭代的iterable中的下一个值。因此,您的价值会丢失而不被使用。

一种解决方案是使用另一个变量(跳至),如:

lst = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
skip_until = -1
for i in range(len(lst)):
    if i == 3:
        skip_until = 9
    if skip_until >= i:
        continue      
    print((i, lst[i]))

输出:

(0, 1) (1, 2) (2, 3) (10,11) (11、12) (12、13) (13、14) (14、15) (15、16)

Laughing Vergil
Reply   •   8 楼
Laughing Vergil    5 年前

您可以使用 continue 声明:

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
for i in range(len(li)):
    if 3 <= i < 10: #along with other condition
        continue
    print(i)

打印时输出 i :

0,1,2,3,10,11,12,13,14,15

打印时输出 li[i] :

1、2、3、4、11、12、13、14、15、16

这个 持续 语句将在循环的开始处引入,忽略以下所有条件。
你可能想看看 loop control statements