Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建调用自身的函数的最佳方法_Python_Python 3.x - Fatal编程技术网

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

Python 创建调用自身的函数的最佳方法,python,python-3.x,Python,Python 3.x,创建基于条件调用自身的函数的最佳方法是什么?例如,我有一个字典变量“table”,其中包含文本(value)元数据(key),另一个变量包含比较文本“line”。我需要写一个程序,将数据从行和匹配它与“表”值。如果匹配成功,则应输出“表键+行”,否则将从“行”中删除最后一个字并执行相同的操作。表变量和行变量示例如下所示: 表格变量: {'[Base Font : IOHLGA+Trebuchet, Font Size : 3.768, Font Weight : 0.0]': 'Addition

创建基于条件调用自身的函数的最佳方法是什么?例如,我有一个字典变量“table”,其中包含文本(value)元数据(key),另一个变量包含比较文本“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.
[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.
行变量:

{'[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.
[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.
我的逻辑:此逻辑不提供预期输出

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 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.
[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.
预期输出:

{'[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.
[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.

我已经写了一些简单的代码,可以通过必要的编辑替换为您的代码

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

我已经写了一些简单的代码,可以通过必要的编辑替换为您的代码

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

我正在添加另一段代码: 请确保增加递归限制,因为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)      

我正在添加另一段代码: 请确保增加递归限制,因为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)      

嗨@Abhinav,你的代码也有一些缺陷,不能满足我的需要。你可以修改你的输入,或者只是使用我的,然后尝试你的代码。然而,您的代码确实给了我一个如何从函数本身调用函数的想法。我创建它只是为了给出一个想法:)。。这就是为什么我写了“必要的编辑”同意,我确实做了一些修改。这没有帮助。问题是,考虑到复杂性,我的代码变得如此冗长。这就是我在这里寻求帮助的原因。嗨@Abhinav,你的代码也有一些缺陷,不能满足我的需要。你可以修改你的输入,或者只是使用我的,然后尝试你的代码。然而,您的代码确实给了我一个如何从函数本身调用函数的想法。我创建它只是为了给出一个想法:)。。这就是为什么我写了“必要的编辑”同意,我确实做了一些修改。这没有帮助。问题是,考虑到复杂性,我的代码变得如此冗长。这就是我在这里寻求帮助的原因。你能澄清一下你到底在问什么吗?“调用自身的函数”(通常称为“递归函数”)实际上就是这样做的,即它调用自身。这只是定义名称(…):名称(…)。这方面没有任何变化,因此没有“最佳方法”,只有一个明显的方法。你认为什么样的变体会有一个“最佳”的解决方案,什么是你“最佳”的标准?我在给定的例子中使用了最好的方法。我无法递归调用函数以生成预期的输出。我很确定给定代码中唯一需要的更改是递归调用函数。然而,由于我是编程新手,我不确定。正如您所提到的,我已经调用了这个函数,但仍然远远没有得到预期的输出。是否有其他可能的解决方案来获得预期的输出?显示的代码是一个格式稍有错误的语句集合。你能澄清一下你打算把什么部分变成递归函数吗?第23行。我认为会有else语句调用函数以获得所需的输出。你能告诉我你觉得哪个部分变形了吗?有没有哪行代码我们可以跳过或做得更好?你能澄清你到底在问什么吗?“调用自身的函数”(通常称为“递归函数”)实际上就是这样做的,即它调用自身。这只是定义名称(…):名称(…)。这方面没有任何变化,因此没有“最佳方法”,只有一个明显的方法。你认为什么样的变体会有一个“最佳”的解决方案,什么是你“最佳”的标准?我在给定的例子中使用了最好的方法。我无法递归调用函数以生成预期的输出。我很确定给定代码中唯一需要的更改是递归调用函数。然而,由于我是编程新手,我不确定。正如您所提到的,我已经调用了这个函数,但仍然远远没有得到预期的输出。是否有其他可能的解决方案来获得预期的输出?显示的代码是一个格式稍有错误的语句集合。你能澄清一下你打算把什么部分变成递归函数吗?第23行。我认为会有else语句调用函数以获得所需的输出。你能告诉我你觉得哪个部分变形了吗?有哪行代码我们可以跳过或做得更好?