使用递归反转列表,并使用python计算列表中某个元素的出现次数

使用递归反转列表,并使用python计算列表中某个元素的出现次数,python,list,recursion,count,reverse,Python,List,Recursion,Count,Reverse,我试图通过编写一个函数来练习使用递归和列表,该函数接收字符串,将其拆分为一个列表,然后使用递归生成字符串的反向版本,并计算列表中出现搜索项的次数。换句话说,如果用户输入“快速棕色狐狸”,并想知道该句中“duck”出现的频率,程序将输出“fox brown quick the”,后跟“duck is find”(在本例中为0)的次数。似乎我的代码目前正在运行,但是,它不会打印反转和计数函数的结果。有人能解释一下原因吗 此函数反转字符串中元素的顺序 def REVERSE(strng): n

我试图通过编写一个函数来练习使用递归和列表,该函数接收字符串,将其拆分为一个列表,然后使用递归生成字符串的反向版本,并计算列表中出现搜索项的次数。换句话说,如果用户输入“快速棕色狐狸”,并想知道该句中“duck”出现的频率,程序将输出“fox brown quick the”,后跟“duck is find”(在本例中为0)的次数。似乎我的代码目前正在运行,但是,它不会打印反转和计数函数的结果。有人能解释一下原因吗

此函数反转字符串中元素的顺序

def REVERSE(strng):
    new_list=''
#base case
    if len(strng)==0:
        return new_list+""
#recursive call
#returns the first element in the string, and adds it to the end of the rest of the string called recursively from the second element
    else:
        new_list+=REVERSE(strng[1:]) + strng[0]
        return new_list
    print (new_list)
def COUNT(strng,srch):
        count=0
    #base case
        if len(strng)==0:
            return count
    #recursive call in event search term found in the first element of the list
        elif strng[0]==srch:
            count+=1
            return COUNT(strng[1:],srch)
    #recursive call in event search term not found in first element of list
        else:
            count+=0
            return COUNT(strng[1:],srch)   
        print ("The term" + srch + "occurs" + count + "times")
函数计算字符串中子字符串的出现次数

def REVERSE(strng):
    new_list=''
#base case
    if len(strng)==0:
        return new_list+""
#recursive call
#returns the first element in the string, and adds it to the end of the rest of the string called recursively from the second element
    else:
        new_list+=REVERSE(strng[1:]) + strng[0]
        return new_list
    print (new_list)
def COUNT(strng,srch):
        count=0
    #base case
        if len(strng)==0:
            return count
    #recursive call in event search term found in the first element of the list
        elif strng[0]==srch:
            count+=1
            return COUNT(strng[1:],srch)
    #recursive call in event search term not found in first element of list
        else:
            count+=0
            return COUNT(strng[1:],srch)   
        print ("The term" + srch + "occurs" + count + "times")
这是调用这两个函数的程序。我将它们保存在单独的文件中以练习导入etc

from functions import *
def main():
        terms = input ("Enter the list of terms:\n").split(" ")
    query = input("Enter a query term:\n")
    print("List in reverse order:")
    REVERSE(terms)
    print()
    COUNT(terms, query)
main()

REVERSE
COUNT
都不会执行这些打印语句-它们总是在执行到达
print
s之前
return

您可能希望从
反向
计数
中删除无用的
打印
s,并在
中打印其返回值:

def main():
terms = input ("Enter the list of terms:\n").split(" ")
query = input("Enter a query term:\n")
print("List in reverse order:")
print(REVERSE(terms))
print()
print(COUNT(terms, query))

到目前为止,我认为以下几点可能对你有所帮助

#!/usr/bin/python

def REVERSE(strng):
    new_list=''
    #base case
    if len(strng)==0:
        new_list+=""
    #recursive call
    #returns the first element in the string, and adds it to the end of the rest of the string called recursively from the second element
    else:
        new_list+=REVERSE(strng[1:]) + strng[0] + " "
    return new_list


def COUNT(strng,srch):
        count=0
        #base case
        if len(strng)==0:
            count = 0
        #recursive call in event search term found in the first element of the list
        elif strng[0]==srch:
            count+=1
            COUNT(strng[1:],srch)
        #recursive call in event search term not found in first element of list
        else:
            count+=0
            COUNT(strng[1:],srch)
        return count

if __name__ == '__main__':
    terms = input ("Enter the list of terms:\n").split(" ")
    query = input("Enter a query term:\n")
    print("List in reverse order: %s" % REVERSE(terms))
    print ("The term '%s' occurs %d times." % (query, COUNT(terms, query)))