Skip to main content
 首页 » 编程设计

python之使用递归函数处理构建字符串

2024年10月17日24lidabo

我有一个打印字符串的递归函数。 我希望该函数返回它当前打印的字符串。

代码基本上打印出我想要的不同递归级别的字符串的不同部分。

我在考虑使用全局变量,但不认为这听起来很 pythonic。

作为大型程序的一部分,我将多次调用此函数。

如果您想查看当前代码:

def check_if_multiple_sections(the_string): 
    level = 0 
    counter = 0 
    for char in the_string: 
        if char == '(': 
            level = level + 1 
            if level == 0: 
                counter = counter + 1 
        if char == ')': 
            level = level - 1 
            if level == 0: 
                counter = counter + 1 
    return counter 
 
def break_it_up(the_string): 
    level = 0 
    counter = 0 
    break_points = [] 
    for char in the_string: 
        if char == '(': 
            level = level + 1 
        if char == ')': 
            level = level - 1 
            if level == 0: 
                break_points.append(counter + 1) 
        counter = counter + 1 
    return break_points 
 
 
def recur_markov_change(the_string, key): 
    #print("the_string: " + str(the_string) + " : key: " +str(key)) 
    if key is not None: 
        #print(the_string) 
        #print("YEA") 
        print(str(the_string).split(' ')[0] +'^'+str(key) + ' ', end='') 
    else: 
        print('(TOP ', end ='') 
    key = ((the_string.split(' '))[0])[1:] 
 
    if len(the_string) < 2: 
        return 
    remaining_string = the_string.split(' ', 1)[1][:-1] 
    results = [] 
    results.append(((remaining_string.split(' '))[0])[1:]) 
    level = 0 
    counter = 0 
    if the_string.count('(') == 1: 
        items = the_string[1:-1] 
        both = items.split(' ') 
        print(str(the_string).split(' ',1)[1], end="") #This prints leaves 
        return 
 
    for char in remaining_string: 
        if char == '(': 
            level = level + 1 
        if char == ')': 
            level = level - 1 
        if level == 0 and char == ' ': 
            results.append(((remaining_string[counter+1:].split(' '))[0])[1:]) 
        counter = counter + 1 
    answer = (key, results, remaining_string) 
    #print(answer) 
 
    if check_if_multiple_sections(remaining_string) > 1: 
        break_points = break_it_up(remaining_string) 
        sublines = [] 
        prev_spot = 0 
        for breaks in break_points: 
            sublines.append(remaining_string[prev_spot:breaks].strip()) 
            prev_spot = breaks 
        sublines.append(remaining_string[breaks:].strip()) 
        for line in sublines: 
            if len(line) > 2: 
                print(' ', end='') 
                recur_markov_change(line, key) 
    print(')', end='') 
 
 
given_string = '(TOP (SBARQ (WHNP_WDT  Which) (SQ_VP (VBZ  is) (ADJP_JJ  last))) (PUNC  ?))' 
string_that_I_want_function_to_return = '(TOP (SBARQ^TOP (WHNP_WDT^SBARQ  Which) (SQ_VP^SBARQ (VBZ^SQ_VP  is) (ADJP_JJ^SQ_VP  last))) (PUNC^TOP  ?))' 
 
recur_markov_change(given_string, None) 
print("\ndesired string:") 
print(string_that_I_want_function_to_return) 

请您参考如下方法:

首先,我添加了一个基本字符串,该函数递归地添加到该字符串中。我调用了这个 initial ,然后我用 inititial+= 替换了所有打印,然后在最后返回了 initial 。

def check_if_multiple_sections(the_string): 
    level = 0 
    counter = 0 
    for char in the_string: 
        if char == '(': 
            level = level + 1 
            if level == 0: 
                counter = counter + 1 
        if char == ')': 
            level = level - 1 
            if level == 0: 
                counter = counter + 1 
    return counter 
 
def break_it_up(the_string): 
    level = 0 
    counter = 0 
    break_points = [] 
    for char in the_string: 
        if char == '(': 
            level = level + 1 
        if char == ')': 
            level = level - 1 
            if level == 0: 
                break_points.append(counter + 1) 
        counter = counter + 1 
    return break_points 
 
 
def recur_markov_change(the_string, key, initial): 
    #initial+=("the_string: " + str(the_string) + " : key: " +str(key)) 
    if key is not None: 
        #initial+=(the_string) 
        #initial+=("YEA") 
        initial+=(str(the_string).split(' ')[0] +'^'+str(key) + ' ') 
    else: 
        initial+=('(TOP ') 
    key = ((the_string.split(' '))[0])[1:] 
 
    if len(the_string) < 2: 
        return 
    remaining_string = the_string.split(' ', 1)[1][:-1] 
    results = [] 
    results.append(((remaining_string.split(' '))[0])[1:]) 
    level = 0 
    counter = 0 
    if the_string.count('(') == 1: 
        items = the_string[1:-1] 
        both = items.split(' ') 
        initial+=(str(the_string).split(' ',1)[1]) #This initial+=s leaves 
        return initial 
 
    for char in remaining_string: 
        if char == '(': 
            level = level + 1 
        if char == ')': 
            level = level - 1 
        if level == 0 and char == ' ': 
            results.append(((remaining_string[counter+1:].split(' '))[0])[1:]) 
        counter = counter + 1 
    answer = (key, results, remaining_string) 
    #initial+=(answer) 
 
    if check_if_multiple_sections(remaining_string) > 1: 
        break_points = break_it_up(remaining_string) 
        sublines = [] 
        prev_spot = 0 
        for breaks in break_points: 
            sublines.append(remaining_string[prev_spot:breaks].strip()) 
            prev_spot = breaks 
        sublines.append(remaining_string[breaks:].strip()) 
        for line in sublines: 
            if len(line) > 2: 
                initial+=(' ') 
                initial = recur_markov_change(line, key, initial) 
    initial+=(')') 
    return initial 
 
 
given_string = '(TOP (SBARQ (WHNP_WDT  Which) (SQ_VP (VBZ  is) (ADJP_JJ  last))) (PUNC  ?))' 
string_that_I_want_function_to_return = '(TOP (SBARQ^TOP (WHNP_WDT^SBARQ  Which) (SQ_VP^SBARQ (VBZ^SQ_VP  is) (ADJP_JJ^SQ_VP  last))) (PUNC^TOP  ?))' 
 
print(recur_markov_change(given_string, None, "")) 
print("\ndesired string:") 
print(string_that_I_want_function_to_return) 

对我来说测试和工作就像一个魅力