Python 如何使用递归拆分字符串列表并返回元组?

Python 如何使用递归拆分字符串列表并返回元组?,python,list,Python,List,一个我遇到麻烦的函数是split_list,我必须通过递归将一个列表分成三个元组,每个元组包含一个strlist。第一个有字符串的元组必须以元音开头,第二个有字符串的元组必须以辅音开头,第三个有字符串的元组不应以字母字符开头 class Node: def __init__(self, value, rest): self.value = value self.rest = rest def __eq__(self, other):

一个我遇到麻烦的函数是split_list,我必须通过递归将一个列表分成三个元组,每个元组包含一个strlist。第一个有字符串的元组必须以元音开头,第二个有字符串的元组必须以辅音开头,第三个有字符串的元组不应以字母字符开头

class Node:
    def __init__(self, value, rest):
        self.value = value
        self.rest = rest
    def __eq__(self, other):
        return ((type(other) == Node)
          and self.value == other.value
          and self.rest == other.rest
        )
    def __repr__(self):
        return ("Node({!r}, {!r})".format(self.value, self.rest))

# a StrList is one of
# - None, or
# - Node(string, StrList)

def split_list(strlist):
    if strlist is None:
       return (None, None, None)
    res = split_list(strlist.rest)
    if strlist.value or res(strlist.value) == 'AEIOUaeiou':
       return (Node(strlist.value, res[0]), res[1], res[2])
    if strlist.value or res(strlist.value) != 'AEIOUaeiou':
       return (res[0], Node(strlist.value, res[1]), res[2])
    if strlist.value.isalpha() or res(strlist.value) == False:
       return (res[0], res[1], Node(strlist.value, res[2]))
这就是我目前所拥有的,但主要问题是我在运行单元测试时没有得到正确的输出。我的问题是断言错误

示例:

strlist = Node("xyz", Node("Abc", Node("49ers", None)))
self.assertEqual(split_list(strlist),(Node('Abc', None), Node('xyz', None), Node('49ers', None)))

strlist = Node("Yellow", Node("abc", Node("$7.25", Node("lime", Node("42", Node("Ethan", None))))))
self.assertEqual(split_list(strlist),(Node('abc', Node("Ethan", None)), Node('Yellow', Node("lime", None)), Node('$7.25', Node("42", None))))
输出:

AssertionError: Tuples differ: (Node('Yellow', Node('abc', Node('$7.25', Node('[50 chars]None) != (Node('abc', Node('Ethan', None)), Node('Yellow'[50 chars]ne)))

有人能解决这个问题吗?非常感谢。

您应该只对遍历部分使用递归。递归函数不应包含3个特定且不同的过程条件

您可以做的是编写一个通用的节点过滤器函数,它将构建一个新的节点链,并使用它来组装元组。如果没有特定条件,此功能将非常简单且易于测试:

def filter(self,condition): # on the Node class
    nextFound = self.rest.filter(condition) if self.rest else None
    return Node(self.value,nextFound) if condition(self) else nextFound 
然后,您可以使用具有不同参数的过滤器函数构建元组:

def split_list(strlist):
    return ( strlist.filter(lambda n:n.value[:1].upper() in "AEIOU"), 
             strlist.filter(lambda n:n.value[:1].upper() in "BCDFGHJKLMNPQRSTVWXYZ"), 
             strlist.filter(lambda n:not n.value[:1].isalpha()) )
[编辑]如果您必须在一个函数中完成所有操作,您可以将这两个功能结合起来(尽管可能很难实现):


如果这是一个家庭作业,我真诚地希望你的老师将促进关注点的分离,而不是要求有问题的编码实践

你应该只对遍历部分使用递归。递归函数不应包含3个特定且不同的过程条件

您可以做的是编写一个通用的节点过滤器函数,它将构建一个新的节点链,并使用它来组装元组。如果没有特定条件,此功能将非常简单且易于测试:

def filter(self,condition): # on the Node class
    nextFound = self.rest.filter(condition) if self.rest else None
    return Node(self.value,nextFound) if condition(self) else nextFound 
然后,您可以使用具有不同参数的过滤器函数构建元组:

def split_list(strlist):
    return ( strlist.filter(lambda n:n.value[:1].upper() in "AEIOU"), 
             strlist.filter(lambda n:n.value[:1].upper() in "BCDFGHJKLMNPQRSTVWXYZ"), 
             strlist.filter(lambda n:not n.value[:1].isalpha()) )
[编辑]如果您必须在一个函数中完成所有操作,您可以将这两个功能结合起来(尽管可能很难实现):


如果这是一个家庭作业,我真诚地希望您的老师将促进关注点的分离,并且不要求有问题的编码实践

如果strlist.value或res(strlist.value)==“AEIOUaeiou”:
对于给定的示例,此条件是有效的,因为值是xyz,所以在输出中第一个元素是“xyz”在第一个示例中
如果strlist.value或res(strlist.value)='AEIOUaeiou':
对于给定的示例,此条件是有效的,因为值为xyz,所以在输出中第一个元素为“xyz”。在第一个示例中,是否有更简单的方法来运行此代码,而无需使用新的筛选函数和lambda?当然,您可以使用与filter函数相同的技术直接从split_列表生成元组。我将添加一个示例…是否有一种更简单的方法可以在不使用新的筛选函数和lambda的情况下运行此代码?当然,您可以使用与筛选函数相同的技术直接从split_列表生成元组。我将添加一个示例。。。