Python 一行中的if和for循环

Python 一行中的if和for循环,python,excel,openpyxl,Python,Excel,Openpyxl,我正在通过openpyxl接近excel,我需要在同一行代码中执行elif语句和for循环 我想要实现的是: 检查值是否为None,如果不是None,则在要查找匹配值索引的列中执行循环。 如果在此列中找不到值,请检查第二列,其中的值为。我必须创建一个elif语句,它将引导机器做类似于它在“else:”语句中所做的事情,我可以处理它,就像我可以在一个链和多行中写入它一样 我的守则如下: for each in sheet['G'][1:]: indexing_no = int(sheet[

我正在通过openpyxl接近excel,我需要在同一行代码中执行elif语句和for循环

我想要实现的是:

检查值是否为None,如果不是None,则在要查找匹配值索引的列中执行循环。 如果在此列中找不到值,请检查第二列,其中的值为。我必须创建一个elif语句,它将引导机器做类似于它在“else:”语句中所做的事情,我可以处理它,就像我可以在一个链和多行中写入它一样

我的守则如下:

for each in sheet['G'][1:]:
    indexing_no = int(sheet['G'].index(each)+1)
    indexing_column = int(sheet['G'].index(each))

    if each.value == None:
        pass
    else:
        for search_value in sheet['A'][1:]:
            if each.value == search_value.value:
                index_no = int(sheet['A'].index(search_value) + 1)
                sheet['H{name}'.format(name = indexing_no)].value = sheet['B{name}'.format(name = index_no)].value
你可以试试这个:

columns = ("G","A","D") # column letters to search in parallel
values  = ( ((c,v) for v in sheet[c][:1]) for c in columns )
for row,colVal in enumerate(zip(*values),1)
    col,value = next( ((c,v) for c,v in colval if v is not None),("",None))
    if not col: continue # or break when no column has any value
    # ...
    # perform common work on first column that has a non-None value
    # using col as the column letter and value for the value of the cell
    # at sheet[col][row]

我需要在同一行代码中执行elif语句和for循环。为什么需要这个?因为在else语句中,我只循环了一列。我需要创建条件来检查第一列,如果在第一列中找不到,则检查另一列。听起来您也应该在这些列上循环。我需要执行以下操作:if each.value in for search_value in sheet['D'][1:]: