创建调用自己的函数的最佳方法

时间:2020-09-19 12:15:28

标签: python python-3.x

创建基于条件调用自身的函数的最佳方法是什么?例如,我有一个字典变量说“表”,其中包含文本(值)元数据(键),而另一个变量则包含比较文本,即“ line”。我需要编写一个程序,该程序将从行中获取数据并将其与“表”值进行匹配。如果匹配成功,则应输出“ table-key + line”,否则将从“ line”中删除最后一个单词并执行相同的操作。表变量和行变量的示例如下:

表变量:

{'[Base Font : IOHLGA+Trebuchet, Font Size : 3.768, Font Weight : 0.0]': 'Additions based on tax 
positions related to the They believe that it is reasonably possible that approximately $40 million of 
its unrecognized tax benefits may be recognized by the end of 2019 as a result of a lapse of the statute 
of limitations or resolution with the tax authorities.', [Base Font : IOFOEO+Imago-Book, Font Size : 6.84, 
Font Weight : 0.0]': 'Additions based on tax positions related to prior'}

行变量:

Additions based on tax positions related to the Additions based on tax positions related to prior Bunge 
believes that it is reasonably possible that approximately $40 million of its unrecognized tax benefits 
may be recognized by the end of 2019 as a result of a lapse of the statute of limitations or resolution 
with the tax authorities.

我的逻辑:此逻辑未提供预期的输出

with open("myfile1.txt","r", encoding='utf-8') as f, open("myfile2.txt","w") as f1:
for line in f:
    if line.strip():
        words = " ".join(line.split())
        for i in table:
            if words in table[i]:
                f1.write(i+line)
                break
            else:
                pass
        else:
            line_split = line.split()
            remaining_itemlist = []
            while len(line_split)>0:
                for i in table:
                    words = " ".join(line_split[:len(line_split)])
                    if words in table[i]:
                        f1.write(i+words)
                        new_word = words.split()
                        for i in range(len(new_word)):
                            line_split.pop(0)
                        for i in table:
                            if " ".join(remaining_itemlist) in table[i]:
                                f1.write(i+" ".join(remaining_itemlist))
                                break
                        break
                    else:
                        pass
                else:
                    remaining_itemlist.insert(0, line_split.pop(-1))
    else:
        f1.write(line)

我的输出:

[Base Font : IOHLGA+Trebuchet, Font Size : 3.768, Font Weight : 0.0]Additions based on tax positions 
related to the Additions based on tax positions related to prior They believe that it is reasonably 
possible that approximately $40 million of its unrecognized tax benefits may be recognized by the end of 
2019 as a result of a lapse of the statute of limitations or resolution with the tax authorities.

预期输出:

[Base Font : IOHLGA+Trebuchet, Font Size : 3.768, Font Weight : 0.0]Additions based on tax positions 
related to the [Base Font : IOFOEO+Imago-Book, Font Size : 6.84, Font Weight : 0.0]Additions based on tax 
positions related to prior [Base Font : IOHLGA+Trebuchet, Font Size : 3.768, Font Weight : 0.0]They believe that it 
is reasonably possible that approximately $40 million of its unrecognized tax benefits may be recognized 
by the end of 2019 as a result of a lapse of the statute of limitations or resolution with the tax authorities.

2 个答案:

答案 0 :(得分:0)

我已经写了一些简单的代码,可以通过必要的修改将其替换为您的代码

line='there is some line which is needed to be checked'

your_dict = {'key1':'there is some line','key2':'it is not','key3':'not even this'}

def check_the_match(line):
    flag=0
    for k in your_dict.keys():
        if line in your_dict[k]:
            flag=1
            print(k+line)
        
    if flag==0:
        print('Checking again')
        new_line = " ".join(line.split()[:-1])
        check_the_match(new_line)

check_the_match(line) 

 

我得到以下输出:

Checking again
Checking again
Checking again
Checking again
Checking again
Checking again
key1there is some line

答案 1 :(得分:0)

我要添加另一段代码: 请确保您增加了递归限制,因为python的递归限制较低。 您可以使用sys库播放递归限制 我是基于对问题的理解而做出的,道歉是任何错误

import sys
sys.setrecursionlimit(1000)

dictionary={
                '[Base Font : IOHLGA+Trebuchet, Font Size : 3.768, Font Weight : 0.0]': 'Additions based on tax positions related to the They believe that it is reasonably possible that approximately $40 million of its unrecognized tax benefits may be recognized by the end of 2019 as a result of a lapse of the statute of limitations or resolution with the tax authorities.',
                '[Base Font : IOFOEO+Imago-Book, Font Size : 6.84, Font Weight : 0.0]': 'Additions based on tax positions related to prior'
            }


line ='Additions based on tax positions related to the Additions based on tax positions related to prior Bunge believes that it is reasonably possible that approximately $40 million of its unrecognized tax benefits may be recognized by the end of 2019 as a result of a lapse of the statute of limitations or resolution with the tax authorities.'


                           
def check_the_match(line):
    
    for k in dictionary.keys():
        if line in dictionary[k]:
            print(k+dictionary[k])
     
    # trimming the line
    new_line = " ".join(line.split(' ')[:-1])
    
    
    if len(new_line.split(' ')[:-1])>1:
        
        check_the_match(new_line)

        
check_the_match(line)