【发布时间】:2015-08-18 18:14:46
【问题描述】:
我有一个表格对象。
我想检查第一行的值是否为Test,在这种情况下,我需要对表中的每一行进行处理。
否则,如果第一行没有Test 的值,我需要跳过该行并处理第 1 行及以后的内容。
因为我需要索引和行,所以我必须使用枚举,但似乎我以一种凌乱的方式使用它。在这里,我两次调用enumerate 并检查索引是否为0 两次。有没有更简洁的方法?
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
【问题讨论】:
-
如果second 行的值为
Test,会发生什么?如果它是第一行,它看起来会触发相同的特殊处理。无论如何,我建议跳过循环中的第一行(首先专门处理它)并使用enumerate(..., start=1)来处理其余部分。