Python如何使用枚举和列表跳过第一次(即零次)迭代?

Python如何使用枚举和列表跳过第一次(即零次)迭代?,python,list,enumerate,Python,List,Enumerate,我有一张桌子 我想检查第一行的值是否为Test,在这种情况下,我需要对表中的每一行执行一些操作 否则,如果第一行没有Test的值,我需要跳过该行,对第1行和第1行执行一些操作 因为我需要索引和行,所以我必须使用enumerate,但我似乎在混乱地使用它。这里我调用enumerate两次,并检查索引是否02次。有没有更简洁的方法 for i, r in enumerate(null_clipData.rows()): if r[0].val == 'Test': # Do

我有一张桌子

我想检查第一行的值是否为
Test
,在这种情况下,我需要对表中的每一行执行一些操作

否则,如果第一行没有
Test
的值,我需要跳过该行,对第1行和第1行执行一些操作

因为我需要索引和行,所以我必须使用enumerate,但我似乎在混乱地使用它。这里我调用
enumerate
两次,并检查索引是否
0
2次。有没有更简洁的方法

for i, r in enumerate(null_clipData.rows()):
    if r[0].val == 'Test':
        # Do something if the first row is Test
        for index, row in enumerate(null_clipData.rows()):
            if index == 0:
                continue # But do anything the first time (stay in this loop though)
            print(index, row)
        break # All through with test row

    if i == 0:
        continue # Don't do anything with the first row if the value was not Test

    print(i, r) # Do something with i and r

按照Kevin的建议,您可以单独处理第一项,然后继续循环:

rows = iter(null_clipData.rows())

firstRow = next(rows)
specialHandling = firstRow.val == 'Test'

for i, r in enumerate(rows, start=1):
    if specialHandling:
        # do something special with r
    else:
        # do the normal stuff with r
或者,也可以将其保持在单个循环中:

specialHandling = False # default case
for i, r in enumerate(null_clipData.rows()):
    if i == 0: # first item
        specialHandling = r == 'Test'
        continue

    if specialHandling:
        # do something special with r
    else:
        # do the normal stuff with r

按照Kevin的建议,您可以单独处理第一项,然后继续循环:

rows = iter(null_clipData.rows())

firstRow = next(rows)
specialHandling = firstRow.val == 'Test'

for i, r in enumerate(rows, start=1):
    if specialHandling:
        # do something special with r
    else:
        # do the normal stuff with r
或者,也可以将其保持在单个循环中:

specialHandling = False # default case
for i, r in enumerate(null_clipData.rows()):
    if i == 0: # first item
        specialHandling = r == 'Test'
        continue

    if specialHandling:
        # do something special with r
    else:
        # do the normal stuff with r
像这样的? 我建议您将这两个不同的for循环分解成各自的函数,以保持其更简洁

刚刚注意到Poke的回答也是:)

像这样的? 我建议您将这两个不同的for循环分解成各自的函数,以保持其更简洁


刚刚注意到Poke的回答也是:)

如果第二行的值为
Test
,会发生什么?如果它是第一行,看起来会触发相同的特殊处理。在任何情况下,我都建议跳过循环中的第一行(首先专门处理它),并使用
enumerate(…,start=1)
处理其余的行。如果第二行的值为
Test
,会发生什么情况?如果它是第一行,看起来会触发相同的特殊处理。无论如何,我建议跳过循环中的第一行(首先专门处理它),并使用
enumerate(…,start=1)
处理其余的行。这假设
rows
是一个序列。这假设
rows
是一个序列。你喜欢这里吗,是不是还有一个蟒蛇?我总是忘记使用iter,前者使它看起来更为独立,这样可能会使你的意图更清晰。但是我个人接受这两种解决方案。另外,在第二个示例中,如果
r==“Test”
语句没有跳过
if specialHandling
?是的,
continue
语句会跳过循环体的其余部分。您也可以在那里使用
elif specialHandling
,但我想明确的是,第一项的处理就在那里。我跳过了剩下的部分,因为我假设你不想继续做第一行的“正常工作”。是的,你是对的,我不想做“正常工作”,但对于第一项,
specialHandling
设置为
True
,因为
r='Test'
)但是
continue
继续通过
if specialHandling
语句,因此我永远无法实际评估
if specialHandling
sleek。你喜欢这里吗,是不是还有一个蟒蛇?我总是忘记使用iter,前者使它看起来更为独立,这样可能会使你的意图更清晰。但是我个人接受这两种解决方案。另外,在第二个示例中,如果
r==“Test”
语句没有跳过
if specialHandling
?是的,
continue
语句会跳过循环体的其余部分。您也可以在那里使用
elif specialHandling
,但我想明确的是,第一项的处理就在那里。我跳过了剩下的部分,因为我假设你不想继续做第一行的“正常工作”。是的,你是对的,我不想做“正常工作”,但对于第一项,
specialHandling
设置为
True
,因为
r='Test'
)但是
continue
继续通过
if specialHandling
语句,因此我永远无法实际计算
if specialHandling