简体   繁体   English

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

[英]Python how to skip the first (i.e. zero) iteration using enumerate and a list?

I have a table object. 我有一个桌子对象。

I want to check to see if the first row has the value of Test , in which case I need to do something with each row in the table. 我想检查第一行是否具有Test的值,在这种情况下,我需要对表中的每一行进行处理。

Otherwise, if the first row does not have the value of Test I need to skip that row and do something with rows 1 and on. 否则,如果第一行没有Test的值,则需要跳过该行,对第1行及以后的内容执行某些操作。

Because I need both the index and the row, I have to use enumerate, but it seems like I am using it in a messy way. 因为我既需要索引又需要行,所以我必须使用枚举,但是似乎我以一种凌乱的方式使用它。 Here I am calling enumerate twice and checking to see if the index is 0 twice. 在这里,我两次调用enumerate ,并两次检查索引是否为0 Is there a more concise way to to this? 有更简洁的方法吗?

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

Following Kevin's advice, you can handle the first item separately, and then continue with the loop: 按照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

Alternatively, you can also keep it in a single loop: 另外,您也可以将其放在一个循环中:

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
rows=null_clipData.rows()
enumeration=enumerate(rows[1:])
if rows[0]=='Test':
    for idx,row in enumeration:
        print(idx,row)
else:
    for i,r in enumeration:
        print (i,r)

Something like that? 这样的事吗? I would suggest that you factor out the two different for loops into their own functions, to keep it cleaner. 我建议您将两个不同的for循环分解为各自的函数,以使其更简洁。

just noticed Poke's answer, that too :) 刚注意到Poke的回答,那也是:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当值为零时如何使用枚举跳过索引计数? - How to skip Index count using enumerate when value is zero? 使用 gspread 中的 gc.open_by_url 跳过导入语句中的第一行(即添加 header=0) - Skip first line in import statement using gc.open_by_url from gspread (i.e. add header=0) 如何使用枚举字符串列表来测试/运行循环的最后一次迭代 - How to test/run last iteration of loop using enumerate on a list of strings 不使用任何现成库的Python中的神经网络。 - Neural Networks in Python without using any readymade libraries…i.e., from first principles..help! 使用 SQLAlchemy,我可以在查询中加入 Python object(即列表)吗? - Using SQLAlchemy, can I join a Python object (i.e. a list) within a Query? 如何在Python中获取600个值的均值为零且标准偏差为σ= 2 dB的对数正态分布(即以dB为单位的正态分布)? - How to get log-normal distribution (i.e. a normal distribution in dB) with a zero mean and a standard deviation of σ = 2 dB for 600 values in python? 在 Python 中获取唯一单词列表(即,不重复) - Getting a list of unique words (i.e., not duplicates) in Python 如何提取python字典列表的多个值(即提取一个键的所有值)? - How to extract multiple values of a list of python dictionary (i.e. extract all the values for one key)? 如何从包含键作为字典列表的字典中获取第一个键值对(即“类型”:45) - How do i fetch first key value pair (i.e., 'type': 45) from a dictionary which contains key as list with dictionaries 使用 Python 点击,如何添加超过 5 个选项,即超过 5 个? - Using Python Click, how do I add more than 5 options i.e. more than 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM