Skip to main content
 首页 » 编程设计

python之如何同时运行两个for循环并判断输出

2024年12月31日9lori

我正在尝试同时运行两个 for 循环,并从每次循环中做出决定。循环搜索 sql 文件并抓取每一行进行比较。

我曾尝试将循环编织在一起,但没有成功。如果有人对如何执行此操作或如何实现多线程方法有建议。 (我从来没有尝试过多线程)

for row in rows: 
    print(row) 
    KnowNextRow = nextrowfinder(str(row)) 
    print(KnowNextRow) 
 
for perrow in perrows: 
    PerfectNextRow = perrow 
    print(str(PerfectNextRow)) 
Search_Procedure(str(row)) 
 
#Then compare see if  KnowNextRow == PerfectNextRow after each pass else:     {Code} 

如果我用 if 语句运行它,我得到的将是从 for 循环中提取的最后一行的决定。我想做的是比较每一行并逐行做出决定。

请您参考如下方法:

我认为您不需要多线程来获得您想要的结果。只需将输出放入 Python 列表并使用枚举函数循环:

rows_list = [] 
perrows_list = [] 
 
for row in rows: 
    print(row) 
    KnowNextRow = nextrowfinder(str(row)) 
    rows_list.append(KnowNextRow) 
 
for perrow in perrows: 
    PerfectNextRow = perrow 
    xx = (str(PerfectNextRow)) 
    perrows_list.append(xx) 
rows_len = len(rows_list)  #Get length of lists to ensure they are the same 
perlen = len(perrows_list)  
 
for idx, item in enumerate(rows_list): 
    if idx < (perlen - 1) # Check that perrows item exists 
        if item == perrows_list[idx]  
            print("Rows Match At Row# " + idx)