这个功能做什么(它是如何工作的)?

时间:2016-12-08 06:42:19

标签: python

def multi_search(pat_file : open, text_file : open) -> {str:[int]}:
    answer = {(p.rstrip(),re.compile(p.rstrip())):[] for p in pat_file}
    for num,line in enumerate(text_file,1):
        line = line.rstrip()
        for (p,c),lines in answer.items():
            if c.search(line) != None:
                lines.append(num)
    return {p:l for (p,c),l in answer.items()}

enter image description here

该函数有两个列表,但我不确定函数返回的是什么,图中显示了两个列表。

有人可以向我解释这个功能有什么作用以及它是如何工作的?

1 个答案:

答案 0 :(得分:0)

对于第一个文件中的每个模式,该函数查找包含给定模式的第二个文件中的所有行号。返回包含pattern --> list of line numbers的字典。