Python:如何对循环进行嵌套枚举?

时间:2017-04-06 18:10:30

标签: python file for-loop nested-loops enumerate

我有两个文件file1file2,对于每个file1行,我正在尝试检查file2的所有行。

所以我对循环执行了嵌套枚举,但是对file1的所有行检查file2的第一行,程序刚刚完成,而不是转到下一个file1行检查file2的所有行。

这就是我所拥有的:

def testing():
    file1 = open(‘file1.txt’, 'r')
    file2 = open(‘file2.txt’, 'r')

    for index1, line1 in enumerate(file1):
        for index2, line2 in enumerate(file2):
                #              Here it prints the first line of `file1.txt` and after checking against all the lines of `file2.txt` (`for index2, line2 in enumerate(file2.txt)`) it completes, rather then going to the outer loop and proceed looping
                print("THIS IS OUTER FOR LOOP LINE: " + line1 + " WITH LINE NUMBER: " + str(index1))

    file1.close()
    file2.close()

如何针对file1的所有行检查file2的每一行?我在这里做错了什么?

提前感谢您,一定会提前/接受回答

3 个答案:

答案 0 :(得分:2)

file2 的位置推回到每个循环顶部的开头。关闭并重新打开它作为aryamccarthy建议,或者通过简单地移动指针以更干净的方式做它:

file1 = open(‘file1.txt’, 'r')
file2 = open(‘file2.txt’, 'r')

for index1, line1 in enumerate(file1):
    file2.seek(0)  # Return to start of file
    for index2, line2 in enumerate(file2):

答案 1 :(得分:1)

您需要在每次迭代时重新打开--- -- Find out the basename of a file/directory (last element after \ or / -- @return {basename} --- function basename(inputstr) sep = "\\/" local last = nil local t={} ; i=1 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do --t[i] = str --i = i + 1 last = str end return last end --- -- Find out current conda env -- @return {false|conda env name} --- function get_conda_env() env_path = clink.get_env('CONDA_DEFAULT_ENV') if env_path then basen = basename(env_path) return basen end return false end --- -- after conda activate: reset prompt and add conda env name --- function conda_prompt_filter() -- reset to original, e.g. after conda activate destroyed it... if string.match(clink.prompt.value, "{lamb}") == nil then -- orig: $E[1;32;40m$P$S{git}{hg}$S$_$E[1;30;40m{lamb}$S$E[0m -- color codes: "\x1b[1;37;40m" cwd = clink.get_cwd() prompt = "\x1b[1;32;40m{cwd} {git}{hg} \n\x1b[1;30;40m{lamb} \x1b[0m" new_value = string.gsub(prompt, "{cwd}", cwd) clink.prompt.value = new_value end -- add in conda env name local conda_env = get_conda_env() if conda_env then clink.prompt.value = string.gsub(clink.prompt.value, "{lamb}", "["..conda_env.."] {lamb}") end end function fix_lamb() if string.match(clink.prompt.value, "{lamb}") ~= nil then clink.prompt.value = string.gsub(clink.prompt.value, "{lamb}", "λ") end end clink.prompt.register_filter(conda_prompt_filter, 1) clink.prompt.register_filter(fix_lamb, 999) 。将该代码移到循环中。否则,在第一次外部迭代后到达import { Component } from '@angular/core'; @Component({ selector: 'a-selector', template: `<div> <input type="text" class="form-control" id="txtx1" [(ngModel)]="text1"/> <br /> <input type="text" class="form-control" id="txtx" [(ngModel)]="text2"/> <input type="button" class="btn btn-success" (click)="GetData()" /> </div>` }) export class EmployeeComponent { text1: string; text2: string; GetData(): void{ } } 的末尾,并且在下一轮外循环中没有任何东西可以迭代。

答案 2 :(得分:0)

我会将每个文件的每一行保存在单独的列表中

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>1.2.4</version>
</dependency>

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-guice</artifactId>
    <version>1.2.4</version>
</dependency>

然后继续比较列表

with open(file1) as f:
    content_1 = f.readlines()

with open(file2) as f:
    content_2 = f.readline()
相关问题