简体   繁体   English

在递归函数中将字符串转换为列表

[英]convert string to list within recursive function

I wrote this recursive function to check for a specific string in a list and if the string is found, it should be returned reversed within the list but I can't seem to keep the list as a list. 我写了这个递归函数来检查列表中的特定字符串,如果找到了字符串,它应该在列表中反转,但我似乎无法将列表保持为列表。 my output is a joined string. 我的输出是一个连接的字符串。 can someone please suggest how to maintain the list type? 有人可以建议如何维护列表类型?

test_list = ['cat', 'hat', 'bat', 'sat']
test_string = 'bat'

def reverser(some_list, some_string):

    if some_list == []:
        return ''
    elif len(some_list) == 1 and item in some_list == some_string:
        return some_string[::-1] 
    else:
        if some_list[0] == some_string:
            return some_string[::-1] + reverser(some_list[1:], some_string)
        if some_list[0] != some_string:
            return some_list[0] + reverser(some_list[1:], some_string)
reverser(test_list, test_string)

output is: 输出是:

'cathattabsat'

but i would like it to be: 但我希望它是:

['cat', 'hat', 'tab', 'sat']

If you want the function to return a list of strings, you need to make sure that in the base case as well as within the loops, you add the appropriate list and not a string. 如果希望函数返回字符串列表,则需要确保在基本情况和循环内添加适当的列表而不是字符串。

In addition, in this row: 另外,在这一行中:

elif len(some_list) == 1 and item in some_list == some_string:

item is not defined. item未定义。 You can replace this row with: 您可以将此行替换为:

elif len(some_list) == 1 and some_string in some_list

Finally, this will do the job: 最后,这将完成工作:

test_list = ['cat', 'hat', 'bat', 'sat']
test_string = 'bat'


def reverser(some_list, some_string):

    if some_list == []:
        return []
    elif len(some_list) == 1 and some_string in some_list:
        return [some_string[::-1]] 
    else:
        if some_list[0] == some_string:
            return [some_string[::-1]] + reverser(some_list[1:], some_string)
        if some_list[0] != some_string:
            return [some_list[0]] + reverser(some_list[1:], some_string)
reverser(test_list, test_string)

This returns 这回来了

['cat', 'hat', 'tab', 'sat']

Your return statement returns a string always. 您的return语句始终返回一个字符串。

return some_string[::-1] + reverser(some_list[1:], some_string)

Note: some_string[::-1] is a string 注意: some_string[::-1]是一个字符串

Wrap it in [] to make it a list of one item. 将其包裹在[] ,使其成为一个项目的列表。

return [some_string[::-1]] + reverser(some_list[1:], some_string)

However, there's a much simpler way than this weird loop you're doing. 然而,这比你正在做的这个奇怪的循环要简单得多。 You can use map or list comprehensions. 您可以使用地图或列表推导。

def reverse_string(any, match):
    if any == match: return any[::-1]
    else: return any

def reverser(some_list, some_string):
    return [reverse_string(s, some_string) for s in some_list]

Your recursive function is more complicated than it needs to be: 你的递归函数比它需要的更复杂:

def reverser(some_list, some_string):
    if some_list == []:
        return []

    word = some_string[::-1] if some_list[0] == some_string else some_list[0]
    return [word] + reverser(some_list[1:], some_string)

But as mentioned by another answerer, this is much better done with a list comprehension: 但正如另一位回答者所提到的,使用列表理解可以做得更好:

[word[::-1] if word == some_string else word for word in some_list]

Think about the return type you want, and the expression you are returning: 考虑一下您想要的返回类型以及您要返回的表达式:

return some_list[0] + reverser(some_list[1:], some_string)

You have a list of strings. 你有一个字符串列表。 So some_list[0] is going to be a string. 所以some_list[0]将成为一个字符串。 You're returning a string, plus whatever. 你正在返回一个字符串,加上任何东西。

If you want to return a list, make the string into a list somehow: 如果要返回列表,请以某种方式将字符串转换为列表:

return some_list[0:1] + ...
return [some_list[0]] + ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM